Script to delete files based on filename content
Hi,
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:
AAA01022010.xxx
AAA02022010.xxx
AAA04022010.xxx
AAA06032010.xxx
AAA08032010.xxx
AAA02042010.xxx
AAA11042010.xxx
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.
Thanks
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:
AAA01022010.xxx
AAA02022010.xxx
AAA04022010.xxx
AAA06032010.xxx
AAA08032010.xxx
AAA02042010.xxx
AAA11042010.xxx
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.
Thanks
0 Comments
[ + ] Show comments
Answers (1)
Please log in to answer
Posted by:
anonymous_9363
12 years ago
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.
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.

so that the conversation will remain readable.