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.
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:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||