I want to create a batch file that will sort the files(based on created date) in a directory and only keep x number of the most recent files.
The files also have a date/time in their names(ie. file201208140322) so if it’s possible doing this by comparing current date/time to the substring I am open to this as well.
Can anybody help me with the command? Thank you for your help.
You can use
DIR /O-D /B /A-Dto get a list of files sorted in descending modification time, then feed the output of this command toFOR /Fso that you get to process these files. UsingSET /Ayou can increment a counter that keeps track of how many files we have seen so far, and after this counter reaches a certain threshold you can start deleting all subsequent entries.Here’s a batch file that does this:
There are also other possible variations on this theme, so the above is not the only solution.