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!

Most Recent File

Status
Not open for further replies.

MasterRacker

New member
Oct 13, 1999
3,343
US
I'm scripting a check to see if processes have run by looking at folders of log files to see if the most recent file is less than X days old.

This should do what I want
Code:
statusOK = 0

For Each currFile In srcFolder.files
    If DateDiff("d", currFile.DateLastModified, Now) <= CInt (age) Then
		statusOK = 1
		Exit For
    End If
Next
Potentially I could end up looping through the whole collection. Is there any more efficient way to find the most recent file?

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]
 
the collection is enumerated based on the name of the file? so, if your files are created with the least alphbetic last then it will be more efficient :)
 
Sorry,

Here's where the collection comes from:
Code:
set FSO = WScript.CreateObject("Scripting.FileSystemObject")
srcFQN = Wscript.Arguments(0)
set srcFolder = FSO.GetFolder(srcFQN)
The order is whatever the files method returns. (?)

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]
 
yeap, the method returns them in an alphbetical list i believe? therefore if they were created in such as way as the most recent was the lowest in an alphbetical list then this would spend up your iteration (it was a rather tongue in cheek post by myself but ultimately if you are incontrol of the file generation process then i see no reason why it isnt valid)

as an example....if you were to write a taskseq processor, based on xml file being location in a %serialnumber% folder and you wanted to ensure they were parsed first in first out then you would create your taskseqs in the %serialnumber% folder in a format like

000011
000016
000022

etc
 
Not in control of the naming. In one specific case, the names contain the date and therefore the most recent is the last in the list if listed alphabetically.

I'm not sure you can guarantee the order of fso.files anyway, depending on how its coded. I've watched large file copies where the order of the files copied had nothing to do with the name. I suspect that has to do with the physical placement on the source disk.

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]
 
must have been simulatious writes then.
can you iterate through a collection backwards then?

i can more or less state my career that .GetFiles will return them in alph order
 
The Dir command from a Command prompt will allow its list of files to be sorted by date with the /OD option.
 
Iteration order is somewhat irrelevant to what I'm doing. I'm trying to write a reusable script I can feed a folder and age and let it tell me yes or no if something is less than the age given. Different folders will have different naming schemes.

I was just looking for a geek tweak that might be more 'elegant' than simply looping through the entire collection.

Thanks for the suggestions though.

(An example of a "geek tweak" is a guy who spends a full month getting a 3 hour process to run 1 second faster by re-writing part of the compiler. [ROFL] )

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]
 
I'm guessing this is rough because I'm only an occasional scripter but how about;

Folder = "C:\Windows"

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd /c dir " & Folder & "\*.* /OD >myfile.txt"
Set WshShell = Nothing

Dim objFSO 'As FileSystemObject
dim oTS
dim sText

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set oTS = objFSO.OpenTextFile ("myfile.txt")
sText = oTS.ReadAll

oTS.close
set oTS = nothing
Set objFSO = nothing

Lines = split(stext,vbcrlf)

'the last line containing '<DIR>' contains details for the most recent file
for i = 0 to ubound(Lines)
if instr(Lines(i),"<DIR>") then
j=i
end if
next

MsgBox "the most recent file is " & Lines(j),vbokonly,Folder
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top