John W. Krahn wrote:
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.
Thank you Aruna for writing this script.
As I will be running the script on about 3 or 4 folders I was thinking
of making a batch file where I would define the path then run the
script, as there would only be 3 or 4 lines, I don't even need to worry
about loops.
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.
This won't be a problem in my case as none of the files I will be
working on will be in use.
But, food for thought ;-)
thank you
Brian
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/