http://www.perlmonks.org?node_id=35808


in reply to Removing files with extentions of log and date-time.

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.

  • Comment on (Dermot) Re: Removing files with extentions of log and date-time.