Aruna Goke wrote:
brian54321uk wrote:
Aruna Goke wrote:
Mr. Shawn H. Corey wrote:
On Fri, 2008-09-05 at 19:09 +0100, brian54321uk wrote:
HI again
I would like to test a folder full of files, and if a file contains abc123blue or xyz357green then that file is to be deleted.
What would be the best way of achieving this please?

If however, it would be simpler for the script to empty the files contents, that's not a problem as I have another script that I can run which deletes empty files.

thanks
Brian


To delete a file, see `perldoc -f unlink`.
To empty a file, see `perldoc -f truncate`.


do you mean if the content of the file contains "abc123blue || xyz357green" or if the filename contains abc123blue || xyz357green.


goksie



If a file contains the data.



#!/usr/bin/perl

use warnings;
use strict;

# open the directory.

my $dirname = 'path-to-your-directory';

opendir DH, $dirname or die "cannot open $dirname: $!";
while(my $filname=readdir(DH)){
open FH, "<", $filname or die "Cannot open $filname:$!";

You are trying to open $filname in the current directory and not in the $dirname directory where the file actually resides.

   while(<FH>){
   print $filname, if/abc123blue|xyz357green/; #for debugging purpose
   unlink $filname, if/abc123blue|xyz357green/;

On some operating systems you may not be able to unlink the file if the file is still in use. And, of course, you are trying to unlink $filname in the current directory and not in the $dirname directory where the file actually resides.

   }
}


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to