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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.