Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Code for DELETING NT FILES

by rauschy33 (Initiate)
on Jul 24, 2000 at 20:13 UTC ( #24101=perlquestion: print w/replies, xml ) Need Help??

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

thank you for those who responded; Here is the code that I have been working with:
$dir= shift || '.'; opendir DIR, $dir or die "Can't open directory $dir: $!\n"; while ($file= readdir DIR ) { next if $file !~/^\w{10,}\d{4,}\.\d{1,}/; print "Found a file: $file\n" if $file=~/^\w{10,}\d{4,}\.\d{1,}/; } print "are you sure that you want to delete $file 'Y/y(es)' "; chop ($answer = <STDIN>); unlink $file or die "Can't delete $file: $!\n" if $answer =~m%^Y(es)?$ +%i;

Replies are listed 'Best First'.
Buzzcutbuddha: Re: Code for DELETING NT FILES
by buzzcutbuddha (Chaplain) on Jul 24, 2000 at 20:21 UTC
    C:\>perl -w rauschcode.pl C:\WINNT\temp Use of uninitialized value in concatenation (.) at rauschcode.pl line +10. are you sure that you want to delete 'Y/y(es)' y Use of uninitialized value in unlink at rauschcode.pl line 13, <STDIN> + line 1. Use of uninitialized value in concatenation (.) at rauschcode.pl line +13, <STDIN> line 1. Can't delete : Bad file descriptor
    Perhaps you should use File::Find instead. It doesn't look you are capturing the File's name....

    use File::Find; find(\&wanted, (shift || '.')); sub wanted { my $filename = $File::Find::name; print "Delete $filename? (Y/n)"; my $answer = (<STDIN>); unlink $filename if $answer =~m%^Y(es)?$%i; }
    Of course, this could be made much better by adding some error checking, etc, but this seems to work just fine! :) HTH.
RE: Code for DELETING NT FILES
by autark (Friar) on Jul 24, 2000 at 20:43 UTC
    Well, as far as I can tell, you ask the user about the file _outside_ of the while-loop, and you don't take into consideration that $dir might be something else than '.'
    This is how I would have written it (more or less):
    my $dir = shift || '.'; opendir DIR, $dir or die "Can't open directory $dir: $!\n"; while (defined (my $file = readdir DIR) ) { next if $file !~ /^\w{10,}\d{4,}\.\d{1,}/; print "Found a file: $file\n"; print "are you sure that you want to delete $file 'Y/y(es)' " +; chomp( my $answer=<STDIN> ); do { unlink "$dir/$file" or die "Can't delete $file: $!\n" } if $answer =~ m%^Y(es)?$%i; }
    You might have to use the 'while( defined (...) )'-construct if you are using an older perl.

    Autark.

Re: Code for DELETING NT FILES
by tye (Sage) on Jul 24, 2000 at 20:23 UTC

    If $dir isn't ".", then this won't work. unlink($file) tries to delete $file from your current directory, not from whatever $dir is.

    unlink("$dir/$file");

    If you want to port to platforms that don't use "/" as the direcotry separator, I suggest lookin at how MakeMaker handles this.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2021-01-18 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?