Forum Moderators: coopster & phranque

Message Too Old, No Replies

Perl script - one more step?

         

stcrim

1:39 am on Jun 7, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the following will (and it does) delete the files in a directory Is there a way to modify it to delete the files in several directories? Or would I have to run a separate script for each??? -s-

#!/usr/bin/perl
$path="/www/mydomain/cgi-bin/tracking/logs";
opendir(FP,"$path");
@del=readdir(FP);
closedir(FP);
foreach $i (@del) {
unlink "$path/$i";
}

Air

3:40 am on Jun 7, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's one way, there's a more elegant and compact way, but I think this is easiest if you want to add more directories to delete files from:

#!/usr/bin/perl

$path="/www/mydomain/cgi-bin/tracking/logs";
$path1="/www/mydomain/cgi-bin/tracking/logs1";
$path2="/www/mydomain/cgi-bin/tracking/logs2";

#----------------------------------------------#
# Delete files in directory specified by $path #
#----------------------------------------------#
opendir(FP,"$path";
@del=readdir(FP);
closedir(FP);
foreach $i (@del) {
unlink "$path/$i";
}

#----------------------------------------------#
# Delete files in directory specified by $path1#
#----------------------------------------------#
opendir(FP1,"$path1";
@del1=readdir(FP1);
closedir(FP1);
foreach $i (@del1) {
unlink "$path1/$i";
}

#----------------------------------------------#
# Delete files in directory specified by $path2#
#----------------------------------------------#
opendir(FP2,"$path2";
@del2=readdir(FP2);
closedir(FP2);
foreach $i (@del2) {
unlink "$path2/$i";
}

Brett_Tabke

4:44 am on Jun 7, 2001 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



How many ways can I delete thee...

#!/usr/bin/perl

@files =(
"/www/mydomain/cgi-bin/tracking/logs",
"/www/mydomain/cgi-bin/tracking/bar",
"/www/mydomain/cgi-bin/tracking/foo"
);

foreach (@files) {
&delfiles($_);
}

sub delfiles {
my $path =shift;
opendir(FP,"$path");
@del=readdir(FP);
closedir(FP);
foreach $i (@del) {
unlink "$path/$i";
}
}


small bug fix -some missing commas

(edited by: rcjordan at 10:29 pm (gmt) on Nov. 5, 2001)

bobriggs

4:48 am on Jun 7, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm sure it will be a one liner soon.... :)

sugarkane

8:27 pm on Jun 7, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not quite a one liner, but another way:

#!/usr/bin/perl
use File::Find;

@dirs = (
"/www/mydomain/cgi-bin/tracking/logs/",
"/www/mydomain/cgi-bin/tracking/bar/",
"/www/mydomain/cgi-bin/tracking/foo/"
);

find sub {unlink $File::Find::name;},@dirs;

stcrim

9:51 pm on Jun 7, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks gang -

-s-

sugarkane

10:30 pm on Jun 7, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yikes, sorry folks - my last post above won't work in the way I originally posted it. I've hopefully fixed it now...

rcjordan

7:36 pm on Nov 5, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How would I modify stcrim's script to delete only files starting with certain characters, "mtd-" as an example when filenames are mtd-010731, mtd-010830, etc.

Air

8:37 pm on Nov 5, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




Borrowing Brett's code as an example:

#!/usr/bin/perl

@files =(
"/www/mydomain/cgi-bin/tracking/logs",
"/www/mydomain/cgi-bin/tracking/bar",
"/www/mydomain/cgi-bin/tracking/foo"
);

foreach (@files) {
&delfiles($_);
}

sub delfiles {
my $path =shift;
opendir(FP,"$path");
@del=readdir(FP);
closedir(FP);
foreach $i (@del) {
if($i =~ /^mtd-/){unlink "$path/$i"} ;
}
}

The only line changed is: if($i =~ /^mtd-/){unlink "$path/$i"} ; make sure you test first :)



small bug fix -some missing commas

(edited by: rcjordan at 10:27 pm (gmt) on Nov. 5, 2001)

rcjordan

10:31 pm on Nov 5, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>test first

Done! It works. Thanks!
(found a small bug in the above code, it's been fixed.)