Peter Lemus writes ..
>I need to delete some directories, specified in
>removedir.txt, I'll like to check whether the file
>exists or not, if it doesn't I need to print file has
>been deleted. this is what I've done so far.
ok .. line by line (and excuse the pedantry)
>use file::spec;
>use win32;
please use the correct case for modules .. just because Windows ignores case
- doesn't mean Perl programmers should .. that having been said you don't
use either of the above modules
>chdir ("c://admin");
why change directory here ? .. and why the double forward slash ?
>open (FILE1, 'c://admin//remove.txt') || die "can't...: $!\n";
from your spec - the above file is incorrect (and again with the double
forward slashes) .. it should be
open (FILE1, 'c:/admin/removedir.txt') || die "...: $!\n";
>sleep 2;
what ? .. you want your program to run slower ? .. there's no reason to
sleep here
>while (<FILE1>) { # read file line by line.
>my ($user) = split /,/; #assign line output of file to line.
actually - the comment is wrong here .. what this line is doing is assigning
whatever comes before a comma on this line to the variable $user .. if
there's no comma then the whole line is assigned
> chdir ('c://admin//test');
again with the double forward slashes ?
> chomp $user;
yep .. for lines without commas this strips off the EOL character
> rmdir "/s /\q $user";
rmdir is a Perl function .. it doesn't take the same parameters as the DOS
command
perldoc -f rmdir
and it only works if the directory is empty
> # system ('rmdir /s /q $user');
the above attempt would have worked if you'd have used double quotes instead
of single quotes .. double quotes allow variables to be interpolated
(obviously you'd also need to uncomment this line)
> #system ('rmdir /s /q $user'); # remove directory
so you tried it with and without a trailing comment :) .. this line would
also have worked with double quotes
> # if (-d ) { # check if file exist: "please help me here"
> }
the 'rmdir' should be inside that if statement .. although it's harmless if
there's no matching directory .. the DOS rmdir will print to STDERR an error
message which may be undesirable
>print "Specified directory has been deleted \n";
>sleep 2;
again .. slowing the program down - why ?
> }
the end of the while is here I guess .. so if there is more that one
directory on the one line separated by commas (as your split seems to
suggest) then only the first one will be processed
>close FILE1
although the last statement doesn't have to have a trailing semi-colon ..
it's still good practice to use one .. although I don't usually close
filehandles that have been opened for reading .. Perl closes them for you
when it exits
so .. your code could probably have looked something like this (using the
wonderful File::Path module)
#!perl -w
use strict;
use File::Path 'rmtree';
open DIRS, 'c:/admin/removedir.txt' or die "Bad open: $!";
chomp( my @dirs = <DIRS>);
rmtree( \@dirs);
__END__
have fun
--
jason king
In New York, a fine of $25 can be levied for flirting. This old law
specifically prohibits men from turning around on any city street and
looking "at a woman in that way." A second conviction for a crime of
this magnitude calls for the violating male to be forced to wear a
"pair of horse-blinders" wherever and whenever he goes outside for a
stroll. - http://dumblaws.com/