I am writing a script to read a log file that is dumped at random
times. All of the logs are in the same location but the name contains a
timestamp of sorts.

example:
c:\log\log2004-11-16-13-12.log

How do I search the directory for "c:\log\log*" and open the newest one
for reading?

Re: FileExists Wildcard + newest file by McKirahan

McKirahan
Mon Jan 03 11:53:23 CST 2005

"bmills123" <pha3drus@prodigy.net> wrote in message
news:1104770593.396009.176480@z14g2000cwz.googlegroups.com...
> I am writing a script to read a log file that is dumped at random
> times. All of the logs are in the same location but the name contains a
> timestamp of sorts.
>
> example:
> c:\log\log2004-11-16-13-12.log
>
> How do I search the directory for "c:\log\log*" and open the newest one
> for reading?
>

Will this help? Watch for word-wrap.

'*
'* Identify latest .log file
'*
Option Explicit
'*
'* Declare Constants
'*
Const cVBS = "log_file.vbs"
Const cFOL = "C:\Log\"
Const cLOG = "log*.log"
'*
'* Declare Variables
'*
Dim strFIL
Dim strGF2
'*
'* Declare Objects
'*
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objGF1
Dim objGF2
'*
'* Files in Folder
'*
If Not objFSO.FolderExists(cFOL) Then WScript.Quit
Set objGF1 = objFSO.GetFolder(cFOL)
Set objGF2 = objGF1.Files
For Each strGF2 in objGF2
If Left(strGF2.Name,3) & "*" & Right(strGF2.Name,4) = cLOG Then
strFIL = strGF2.Name
End If
Next
'*
'* Destroy Objects
'*
Set objFSO = Nothing
Set objGF1 = Nothing
Set objGF2 = Nothing
'*
'* Results
'*
MsgBox strFIL,vbInformation,cVBS

The "For Each" loop returns file in order by DateCreated.



Re: FileExists Wildcard + newest file by bmills123

bmills123
Mon Jan 03 13:15:46 CST 2005

That helps alot. Thanks!


RE: FileExists Wildcard + newest file by Gary

Gary
Mon Jan 03 13:51:04 CST 2005

Should you want only the lastest file try...

Sub LatestFile
Dim lFile, nFile
Set fldr = fso.GetFolder("C:\LOG")
Set colFiles = fldr.Files
lFile = ""
s = ""

'Search for lastest file created
For Each oFile in colFiles
If nFile = "" Then
nFile = ofile.name
lFile = oFile.DateCreated
Else
If DateDiff("s",lFile,oFile.Datecreated) > "0" Then
nFile = ofile.name
lFile = oFile.DateCreated
End If
End If
Next

'Latest file
wScript.echo nFile
End Sub


"bmills123" wrote:

> I am writing a script to read a log file that is dumped at random
> times. All of the logs are in the same location but the name contains a
> timestamp of sorts.
>
> example:
> c:\log\log2004-11-16-13-12.log
>
> How do I search the directory for "c:\log\log*" and open the newest one
> for reading?
>
>