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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Seaching for latest modified file

Status
Not open for further replies.

pt2huynh

Programmer
Feb 20, 2004
12
0
0
CA
Hi everyone,

I have a folder c:\folder. Inside this folder, there are files: a.log, b.log, data.bak, win.sys etc..

I have a service that generate the log files every few hours.

How do I determine what the latest modified LOG file is using vbscript? Thanks everyone.
 
You can do it like this. Just change the directory information. In this example I am checking the age of files int he root of C:.

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = objFSO.GetFolder("C:\")

seedDate = CDate("1/1/1970")

For Each oFile In oFolder.Files
	If oFile.DateLastModified > seedDate Then
		NewestFile = oFile.Name
		seedDate = oFile.DateLastModified
	End If
Next

WScript.Echo NewestFile

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Thanks Mark for all your help. Actually the file I am looking for is not the latest modified file. Yes, I am trying to find the latest modified log file but the latest modified file is not a log file (it is second or third latest modified file). Can this be done? Thanks in advance.
 
Can you modify the script above just to compare date modified of just the log files (etc. a.log, b.log) and return the latest modified? Thanks.
 
You can use dictionary and adding a simple sorting to get to the data needed. Like this.
[tt]
dim sfolder, sext, fso, odic, a

[green]'your input data - modify them to your need
sfolder="c:\folder"
sext="log"[/green]

set fso=createobject("scripting.filesystemobject")
set odic=createobject("scripting.dictionary")

if not fso.folderexists(sfolder) then
wscript.echo "folder not found"
wscript.quit
end if

for each ofile in fso.getfolder(sfolder).files
if strcomp(fso.getextensionname(ofile),sext)=0 then
odic.add ofile.datelastmodified,ofile.path
end if
next

[green]'sort target file data[/green]
a=array(0,"")
for each skey in odic.keys
if skey>a(0) then
a(0)=skey
a(1)=odic.item(skey)
end if
next
wscript.echo "target file: " & a(1) & vbcrlf & "Last modified: " & a(0)

set odic=nothing
set fso=nothing
[/tt]
 
Thanks tsuji. This script work like a charm. Thanks so much for your help. Thanks everyone.
 
Using my example above, the following will just report the last modified log file.

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = objFSO.GetFolder("C:\")

seedDate = CDate("1/1/1970")

For Each oFile In oFolder.Files
	If Right(oFile.Name,3) = "log" Then
	    If oFile.DateLastModified > seedDate Then
	        NewestFile = oFile.Name
	        seedDate = oFile.DateLastModified
	    End If
	End If
Next

WScript.Echo NewestFile

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top