I was thinking something similar to @SLB but I took a little different approach.
The Code in the Actionscript below that is included in the action was adapted from the example in this forum post by jgstew and Brolly33 that gives a very detailed explaination of how they accomplished the task. My logic uses that to that relevance to give me the path the the newest file and then includes that in the action script to basically exclude that from the creation of the batch file for the other files that do meet the criteria so it essentially gets skipped. I hope this helps.
BigFix relevance to get latest 5 modified .txt file - Content Authoring - BigFix Forum
parameter "newest" = "{(items 2 of items 1 of (((tuple string items ((integers in (it-1,maximum of (it-1;0))) of (number of tuple string items of it)) of it) of concatenation ", " of following texts of firsts ", " of (it as string) of unique values of (it as time) of tuple string items 1 of elements of it), ((tuple string items 0 of it, tuple string items 1 of it, tuple string items 2 of it) of elements of it)) whose (item 0 of it = item 1 of item 1 of it) of set of (it as string) of (name of it, following text of first ", " of (it as string) of modification time of it, pathname of it) of files whose ((name of it as lowercase starts with "esp_") of it) of folder "c:\temp")}"
ActionScript -
// BigFix Action Script: Keep newest esp_* file in C:\temp, delete the rest
// Safety: stop if folder doesn't exist or no matching files
if {not exists folder "c:\temp"}
// or just exit silently - comment out next line if you want to continue anyway
// exit 0
endif
// Collect all matching files into __appendfile (full pathnames)
delete __appendfile
appendfile {concatenation "%0a" of pathnames of files whose (name of it as lowercase starts with "esp_") of folder "c:\temp"}
// If no files were found → exit gracefully
if {number of lines of file "__appendfile" = 0}
exit 0
endif
// identify the newest file that meets the criteria
parameter "newest" = "{(items 2 of items 1 of (((tuple string items ((integers in (it-1,maximum of (it-1;0))) of (number of tuple string items of it)) of it) of concatenation ", " of following texts of firsts ", " of (it as string) of unique values of (it as time) of tuple string items 1 of elements of it), ((tuple string items 0 of it, tuple string items 1 of it, tuple string items 2 of it) of elements of it)) whose (item 0 of it = item 1 of item 1 of it) of set of (it as string) of (name of it, following text of first ", " of (it as string) of modification time of it, pathname of it) of files whose ((name of it as lowercase starts with "esp_") of it) of folder "c:\temp")}"
// Now delete everything except the newest one
delete __appendfile
// Only generate del for files that are NOT the newest one
appendfile {concatenation "%0d%0a" of ("del /Q /F %22" & it & "%22 >nul 2>&1") of pathnames of files whose (name of it as lowercase starts with "esp_" AND pathname of it != parameter "newest") of folder "c:\temp"}
// Optional: echo which one we kept
appendfile echo Kept newest file: %22%parameter "newest"%22
// Write batch file and execute it
delete "C:\temp\~cleanup-esp.bat"
move __appendfile "C:\temp\~cleanup-esp.bat"
waithidden "C:\temp\~cleanup-esp.bat"
delete "C:\temp\~cleanup-esp.bat"
// Optional logging (uncomment if desired)
waithidden cmd.exe /c echo Finished cleanup of esp_ files >> "C:\temp\esp_cleanup.log"