Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

deleting directories

by gitarwmn (Beadle)
on Jul 20, 2005 at 16:26 UTC ( [id://476558]=perlquestion: print w/replies, xml ) Need Help??

gitarwmn has asked for the wisdom of the Perl Monks concerning the following question:


I'm trying to delete a certain set of directories using
use File::Remove qw(remove); remove \1, "dir";

The way the script is setup is to delete all but the 5 oldest. The problem seems to be that its deleting all the directories includign the 5 I don't want deleted. Does anyone have any insight as to why it is deleting everything instead of the directories I'm trying to specify? This is on a Windows system. Thanks

Here is my code...
use strict; use warnings; use Mail::Sendmail; use File::Remove qw(remove); main(); sub main{ my $project = 'crm'; my $purgecnt = 0; my @dirlist = ''; my $OUT = "out.txt"; # generate directoy listing of builds # print "\nGenerating directory output to $OUT...\n"; system("dir /b C:\\builds\\Vnet\\$project\\ > $OUT"); print "\n"; open( FP, "$OUT" ) or die "Can't open $OUT file for reading.\n"; while(my $line = <FP> ){ next if ($line =~ /^\s*$/); # blank lines push (@dirlist, $line); } close FP; #remove last 5 builds from the purge list for(my $i=0; $i < 5; $i++){ chomp (my $temp = pop @dirlist); if ($temp){ print "Skippping: $temp\n"; } } my $ra_dirlist = \@dirlist; #print "directories to delete @$ra_dirlist\n"; # purge each item in dirlist # print "\n"; foreach my $item (@dirlist){ my $tree = "C:\\builds\\Vnet\\$project\\$item"; if($item eq $dirlist[-1]){ print "Purging: $item\n"; remove \1, $tree; last; } $purgecnt++; print "Purging: $item\n"; remove \1, $tree; } print "\n"; print "\nDone: Purged $purgecnt directories\n"; }

Replies are listed 'Best First'.
Re: deleting directories
by Tanktalus (Canon) on Jul 20, 2005 at 16:57 UTC

    First off, I'd suggest getting rid of the system/open/while/close code. Use glob and sort instead. Then there also won't be any \n's.

    Then, instead of popping 5 times, just splice off the end.

    Finally, I'm not sure about File::Remove, but File::Path also has a rmpath function which would do the same trick.

    Debugging trick: don't actually remove anything in your code until you've proven it to work. Use print "Pseudo-deleting $tree"; first. Make sure the five you don't want there aren't there.

Re: deleting directories
by bofh_of_oz (Hermit) on Jul 20, 2005 at 17:55 UTC
    Firstly, drop that main() prototype - it doesn't do anything besides cluttering the code...

    You don't need to list directories by redirecting output of system to a file and reading it - use glob. Here's what you can do:

    use strict; use warnings; use File::Glob; my @dirs; chdir "C:/builds/Vnet/crm"; for (glob("*")) {push @dirs, $_ if -d;} remove \1, $dirs[$_] for (0..$#dirs-5);
    That's supposed to move to the vorking directory, get the list of the subdirectories, and delete everything but the last 5. Assuming you have them properly named, those last 5 directories are what you wanna see untouched.

     

    Note: I never dealt with remove so I'm not sure if it works as you want it to... I tested it with print statement and it worked fine...

    --------------------------------
    An idea is not responsible for the people who believe in it...

Re: deleting directories
by gitarwmn (Beadle) on Jul 20, 2005 at 21:26 UTC
    Very cool, thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://476558]
Approved by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 02:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found