MasterRacker
New member
I have a folder of daily backups file that I will be running a daily cleanup script against. I want to delete all but the most recent 2 or 3 files. I don't want to delete by "date over x days old" because if the backups stopped running for some reason, the most recent files would still disappear. I have the script below working to find the most recent file - so that much of the mechanics is worked out.
What I'm having difficulty with is determining how to get the 2 or 3 most recent files. The only thing I can think of is to store the maxDate as lastMax, loop again comparing to see if curr > Max and < lastMax. Wash, rinse, repeat for 3rd, 4th etc.
Is there a better way?
Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
Code:
dim FSO, srcFQN, srcFolder, currFile, maxFile, maxDate
if Wscript.Arguments.Count < 1 then
Wscript.echo ("USAGE: MostRecent <source folder>")
Wscript.Quit
end if
set FSO = WScript.CreateObject("Scripting.FileSystemObject")
srcFQN = Wscript.Arguments(0)
if not FSO.FolderExists(srcFQN) then
Wscript.Echo ("Folder does not exist: [" & srcFQN & "]")
Wscript.Quit
end if
set srcFolder = FSO.GetFolder(srcFQN)
set maxFile = nothing
maxDate = #1/1/1980#
'----- Loop through starting folder and process files
for each currFile in srcFolder.files
if currFile.DateLastModified > maxDate then
set maxFile = currFile
maxDate = maxFile.DateLastModified
end if
next
if not (maxFile is nothing) then
Wscript.Echo "Most recent file: " & maxFile & " : " & maxFile.DateLastModified
else
Wscript.Echo "No files found"
end if
'----- Cleanup
set FSO = nothing
Wscript.Quit
'================================= END OF FILE =================================
Is there a better way?
Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]