Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Removing files with extentions of log and date-time.

by curtisb (Monk)
on Oct 08, 2000 at 14:42 UTC ( #35802=perlquestion: print w/replies, xml ) Need Help??

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

Can anyone tell me what is wrong with this. I have been working on this and it works like 1 out of 100 times. Do I need to use WIN32 or something. This is going to be ran on a Win NT Server in the future. The directories in the code are only test directories.
#!/usr/bin/perl -w $dir = "c:/mydocu~1/Netscape/Suitespot"; print "Starting Delete Process\n"; print "\n"; opendir(DIR, "$dir") || die "No $dir: $!"; @files = grep(!/^\./, readdir(DIR)); @files = sort @files; foreach(@files) { print "$_\n"; next if ($_ !~ /^\.log$/i); next if ($_ !~ /^\.\d+\w+\-\d+\wm$/i); next if ($_ !~ /^\.txt$/i); unlink "$dir/$_" || print "Unable to delete $_: $!"; } print "\n"; print "Deleted all requested files.\n"; closedir(DIR);
Thanks

Replies are listed 'Best First'.
Re: Removing files with extentions of log and date-time.
by moen (Hermit) on Oct 08, 2000 at 17:26 UTC
    #!/usr/bin/perl -w use strict; my $dir = 'path to directory'; opendir (DIR, "$dir") || die "Cannot open directory: $!\n"; print "Starting Delete Process\n\n"; while ($_ = readdir(DIR)) { print $_, "\n"; unless (/^\./) { if (/\.log$/i or /\.\d+\w+\-\d+\wm$/i or /\.txt$/i) { unlink("$dir/$_") if (-f "$dir/$_") || warn "Cannot delete $_: $!\n"; } } } print "\nDeleted all requested files.\n"; closedir(DIR);
    As Dermot says, im not sure about that time/date expression of yours, so please post the date format.
    I tested this code and it works just fine on my linux box, i guess you have to change the shebang. Both in my and your code.
    This will delete all files with extention log, txt or date/time.
    Update: added unless{} to not touch files that start with "." (didn't know they where allowed in windows ;)
(Dermot) Re: Removing files with extentions of log and date-time.
by Dermot (Scribe) on Oct 08, 2000 at 17:10 UTC
    You want to delete all files in a specific directory with some exceptions. The exceptions are based around the filename.

    Your general approach seems fine. One comment is that the regular expressions to allow the execeptions to the delete are probably not doing what you think they are. I assume that you wish to leave all files with a ".log" or ".datetime" or ".txt" extension. If that is so you should stop using the ^ at the start of the regular expression as this binds the regular expression to the start of the line (in this case a line is equivalent to a filename). This will only allow filenames called ".log" and ".txt" to remain whereas you would like "file.log" and "file.txt" to remain.

    I'm not sure that your date/time regular expression is correct either but we would need to know the date/time format. Could you please post the date/time format being used and we can then check your regular expression.

    So, in summary, drop the caret which is forcing the regular expression to match at the start of the line and the script should work as you intended.

    Update: Just now noticed the !~ so invert the sense or what I said above as you want to delete the log/txy/datestamp files and leave all the rest. Yes, that does make more sense.

Re: Removing files with extentions of log and date-time.
by curtisb (Monk) on Oct 09, 2000 at 00:56 UTC
    Thanks Everyone. Borrowing from what you have suggested, really helped. I now know where I was screwing up at. How can I delete something when I have skipped it first? That was a good question and a wrong way to think. so thank for your help. If you want to see what the rewritten code looks like, here you go.
    #!/usr/bin/perl -w $dir = "(directory path)"; print "Starting Delete Process\n\n"; opendir(DIR, "$dir") || die "No $dir: $!"; @files = grep(!/^\./, readdir(DIR)); @files = sort @files; closedir(DIR); foreach (@files) { print $_, "\n"; if (/\.log$/i || /\.\d+\w+\-\d+\wm$/i || /\.txt$/i) { unlink ("$dir/$_") if (-f "$dir/$_") || print "Unable to delete $_: $!"; } }
    Basicly, this script will now delete any log file, date-time file, or text file found in a predetermined directory. Thanks once again for your help.
RE: Removing files with extentions of log and date-time.
by acid06 (Friar) on Oct 09, 2000 at 02:47 UTC
    Just wondering... wouldn't be easier using del *.log/s than writing a perl script for doing this?

    just my $0.02

    "What once was, was, and won't be again."
      It would be easier to write del *.log/s, however I have to maintain a server that downloads about 500Mb to 1Gb a day from the web. Then it does log rotations every 12 hours. So, I figure that I would try out Perl in my sys. admin. environment. But, thanks for your $0.02, I have already gone down that route. Thanks!!!!
Re: Removing files with extentions of log and date-time.
by curtisb (Monk) on Oct 08, 2000 at 20:36 UTC
    Ok, I appreciate the help. As you have asked I am giving you the syntax for my date-time files. Filename.04Oct-12AM I have one other question. You are talking about the ^ (carrot) in the line
    @files = grep(!/^\./, readdir(DIR));
    right? If so, I removed it and it listed no files when ran, but when I went to that directory all the files where still there.
      That line belongs as is. It's looking for all files that do not start with a literal period.

      Under Windows, the only files likely to do that are the directory aliases '.' and '..' -- and you don't really want to unlink those. Leave that line as is.

      One reason your regexes in the version you posted weren't working, besides the unnecessary caret, was because you didn't have the alternation moen provided. First, you'd skip anything that didn't end it '.log'. Unfortunately, that rule excludes anything that ends in '.txt' or your date format.

      In effect, the only thing left after that first rule would be '.log' files. Unfortunately, that wouldn't match the second rule, and no file would even make it to the third rule. So nothing was getting past those rules. (A file can't end in '.txt' and '.log' simultaneously.)

      I hope this helps.

      We have a slight misunderstanding. I was speaking of the carets in the three regular expressions a little further down. The regular expressions that you use to select which files to delete. They need to be written without the caret as the text that you want to match is not at the start of the line.

      In the grep statement you do need the caret. You need it because you want to exclude files that start with a fullstop (you probably call it a period). So, in the case of the grep you want to match:

      1. Start of line
      2. Followed by a fullstop
      3. Followed by anything at all

      This is different from later on when you want to match:

      1. Anything at all. It might be start of line, it might not.
      2. Followed by a fullstop. This might be just after the start of line. Then again, it might be a few characters further along the string being matched against.
      3. Followed by the letter l, then the letter o, then the letter g.

      Do you see the difference ?

Re: Removing files with extentions of log and date-time.
by ant (Scribe) on Oct 09, 2000 at 16:21 UTC
    I love Perl Monks!! I had a similar program reading in the contents of a directory into an array, but I was using 'split'
    and then evaluating the extension of the file variable, so the code was a lot longer and messier. I have now re-written this code
    and it looks alot smarter and hopefully works quicker. It's always good to see other peoples ideas and see different approaches and
    solutions to problems encountered.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2020-11-29 02:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?