Script to delete files based on filename content


I am searching for a solution regarding the following problem:

I have a folder with files that are generating at the same time (so deleting by create or last modified date is not working).

The file names are something like this:


So, I need a script that would be able to read through the file names, and understand those last 8 characters as a date. Then to keep in the folder only the newest 5 files and delete all the rest.

I guess it's a bit complicated, but I hope someone can point me in the right direction.


0 Comments   [ + ] Show comments

Answers (1)

Posted by: anonymous_9363 12 years ago
Red Belt
Pretty straightforward...

There are probably quicker, much more efficient ways but I like to split problems into logical pieces so that, if you get a wrong result, it's simpler to track down where the error is.

- Use the MID function to extract everything starting at character 4, ending at character 12
- Use either LEFT, RIGHT or MID to split the resulting string into day, month and year parts
- Build a proper date string from these constituent parts
- Compare this date with today minus 5 days
- Act accordingly
- Take your "developer education" baseball bat round to whichever idiot decided on the totally wrong-headed filename format and begin lessons. Anyone with even half a brain, who thought about a naming convention for even a nanosecond, would have realised that AAAYYYYMMDD.xxx is the most sensible format.
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ