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

Hello all, I have written a script to use NT Rmdir to purge any folders in a directory that is more than 14 days. When I try to run the script, I get an error "Cannot find path specified", this occurs at this line of code, print "system command: @args \n";

Is the script looking for the "rmdir"? I can do the same thing if I run it from the command prompt without any errors, but the only difference is, I can't check the age of the folder and act upon the results.

The syntax for "RMDIR" on a NT box is, RMDIR [/S] [/Q] [drive:]path

for example: C:\RMDIR /s C:\Archive\Test1 <--This works if you are executing this from the command prompt on NT.

The script does compare the date of the folders, but will not delete what it finds if the error condition is true, meaning any folders over 14 days. The folders it find will also have subfolders and files in them, that's why I used the /S switch.

I cannot get it to work, do you suggest using anything else beside "RMDIR", or do I need to set the environment varible so perl knows how to call the "RMDIR" from the scipt? Please help!!!
#!/usr/bin/perl -w use strict; die "Wrong Number of Arguments" unless $#ARGV == 1; $sourceDir = $ARGV[0]; $days = $ARGV[1]; #foreach $arg (@ARGV) { # print "one arg is $arg\n"; #} opendir source_DIR, $sourceDir or die "ERROR opening: $sourceDir, $!"; @sourceFiles = readdir source_DIR; closedir source_DIR; foreach $file ( @sourceFiles ) { # print( "sourceFiles $file \n" ); # next if -d $sourceDir . "\\" . $file if ($file eq '.') { # do nothing # print "working dir \n"; } else { if ($file eq '..') { # do nothing # print "parent dir \n"; } else { remove_dir( $sourceDir . "\\" . $file ) if -M $sourceDir . "\ +\" . $file >= $days; } } } # del_file # arg_0 = filename, including full path, to be deleted sub remove_dir { my $file = shift(@_); @args = ( 'rmdir /s /q' , $file ) ; print "system command: @args \n"; system( @args ) == 0 or die "error removing $file!"; } # End move_file

Edit: Added <code> tags. Fixed some formatting. larsen

Replies are listed 'Best First'.
Re: using NT - rmdir cmd to purge aged NT folders
by Albannach (Monsignor) on Aug 02, 2002 at 18:42 UTC
    You will probably find rmtree() (part of File::Path) very useful.

    Update: Sorry fglock, but unlink won't necessarily remove directories (won't at all on my NT box at least) and perldoc -f unlink tells me it's a bad choice in un*x too. You can use rmdir but that only works if the directory is empty, and that does not appear to be the case here.

    --
    I'd like to be able to assign to an luser

      The system is trying to find a program named 'rmdir /s /q'. You must write 'rmdir' instead:

       system ( 'cmd', '/c', 'rmdir', '/s', '/q', $file);

      update: use cmd /c to call the "cmd" shell, which runs rmdir for you.

      By the way, if you would delete files you could use perl's own  unlink() instead.

      update: Albannach told me that  perldoc says:

      "unlink" will not delete directories unless you are superuser and the -U flag is supplied to Perl. Even if these conditions are met, be warned that unlinking a directory can inflict damage on your filesystem. Use "rmdir" instead.

      Use "unlink" only for deleting normal files.