Please, I need your assistance with creating a Bigfix action script to check for files whose names start with “esp_” in a folder “c:\temp” then retain only the file with the newest modification timestamp and delete all the others.
My sincere apologies if there already exists a topic on this, but I have not been able to find it having searched the forum.
This forum posting gives a way to find file by create date so we can use this with some small tweaks to build a list of files to delete excluding the most recent.
For applicability relevance I’d think something along the lines of
Relevance 1(limit to Windows OS only)
windows of operating system
Relevance 2(Limit to devices with the C:\Temp folder)
exists folder "C:\temp" whose (exists files whose (name of it starts with "esp_") of it) /
Relevance 3(Restrict to devices with more than 1 copy of the target filename)
number of files whose (name of it starts with "esp_") of folder "C:\temp" > 1
And for the actionscript
parameter "FilesToDelete" = "{"%22" & concatenation "%22,%22" of (pathnames of files whose (name of it starts with "esp_" and modification time of it < maximum of modification times of files whose (name of it contains "esp_") of parent folder of it) of folder "C:\temp") & "%22"}"
waithidden cmd.exe /c "for %a in ({parameter "FilesToDelete"}) do (del %a)"
You can test this using the FixletDebugger and change the waithidden to wait and /c to /k so you see the CMD prompt output
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.
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"