Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recent Files List 1

Status
Not open for further replies.

MasterRacker

New member
Oct 13, 1999
3,343
US
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.
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 =================================
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]
 
you can crudely store the x most recent files in individual buffers.

Code:
for each currFile in srcFolder.files
    if (currFile.DateLastModified > maxDateA) then
        maxDateC = maxDateB
        maxDateB = maxDateA
        maxDateA = currFile.DateLastModified
    elseif (currFile.DateLastModified > maxDateB) then
        maxDateC = maxDateB
        maxDateB = currFile.DateLastModified
    elseif (currFile.DateLastModified > maxDateC) then
        maxDateC = currFile.DateLastModified
    end if
next

-Geates
 
For the problems related to sorting file system's last modified or created date beyond the "minimax", it was once a show piece using disconnected database in the community and then microsoft's technical articles took it up and made it their own as well.

Check this out, for instance, and I am sure you can expand/adopt to your specific need.
 
I was thinking of using an array, but the recordset and its sort method makes building and sorting the list trivial. I love it and will use it.

So I now have two different ways of actually getting the list of files to load the recordset from. I don't really have time to try both and test, but from a gut feel level: which would be faster: a filesystem object or a wmi query?



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]
 
filesystem would be faster

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top