Filter out the 3 most recent files, not necessarily sequential

(imported topic written by lopezrad)

Does anyone know how to filter out the 3 ‘newest/latest timestamp’ files from a list where they are not necessarily timestamp consecutive in relevance?

i.e files have:






The result list should be



I’m listing them here in order, which it is the most likely scenario, but also may need to be sorted?

  • This is to keep only 3 days worth of files and delete the rest. Once I have result the list, then I can do a batch file to delete those.

(imported comment written by SystemAdmin)

Try this action:

if {number of file of folder “c:\example” > 3}

delete file “c:\example{name of file whose (modification time of it = (minimum of modification times of files of folder “c:\examples”))of folder “c:\examples”}”


(imported comment written by lopezrad)

I think that code will actually delete the oldest file, not the other ones…

Let me expand on what we are trying to do:

There is a file generated every half an hour so there can be several files on the same day. The files will not necessarily being generated daily so there may be days with 10+ files (in 30 mins increments), a week without activity, and then another day with other 10+ files. We need to keep only the 3 latest days (not the 3 latest files).

We can figure out which ones correspond to a day by looking at the filename date (and ignore the time):

Q: unique values of (firsts 8 of (names of files of folder “C:\example”))

A: 20121010 20121210 20121215 20121220 20130101

Afte that, I have only been able to go thru the list with an ugly batch file so wondering if there is a way to do it in relevance:

parameter “fullPath” = “c:\example”

parameter “y” = “{ “%22” & concatenation “%22 %22” of (unique values of (firsts 8 of (names of files whose (name of it contains “.xml”) of folder “C:\example”))) & “%22” }”

delete “C:\example\delOldScans.bat”

//appendfile for /f "skip=3 tokens=" %X in ( {(parameter “y”)} ) do del {parameter “fullPath”}%X

appendfile set ctr=0

appendfile :start

appendfile for %%a in ({(parameter “y”)}) do call :maybemove “%%a”

appendfile goto :eof

appendfile :maybemove

appendfile set /a ctr+=1

appendfile if %ctr% leq 3 goto :eof

appendfile del {parameter “fullPath”}%~1*

appendfile goto :eof

move __appendfile “C:\example\delOldScans.bat”

(imported comment written by lopezrad)

Although I’m realizing this will not work either because the ‘name of files’ return the files in oldest to newest order and I need it the other way around. Mmhh

(imported comment written by SystemAdmin)

My idea is, we try to use “modification time” or “creation time” instead of the substring of the file name.

have you tried this?

names of files whose (modification time of it < now - 3*day and name of it ends with “.xml”) of folder “c:\example”