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

FSO - Return Newest File in Folder 2

Status
Not open for further replies.

dougcranston

Technical User
Oct 5, 2001
326
US
I am really having a mental block on this.

I want to identify the newest file in a specific directory.

Specifically, it will be the newest NTBackup log file created during nightly backups. Since Win2k does not permit you to define the log file name, I need some way to identify it so I can save a copy elsewhere so I have more than the 10 backup logs it will maintain.

Also, once I can identify that file then I can email it to myself and a backup admin to review how the backup worked.

But I can't seem to even know where to start on this one..

Any suggestions on where I might find some sample code I could adapt?

Thanks inadvance.

Dougc
 
dougcranston,

This illustrates the method.
Code:
pathspec="d:\test\log"    'your input
info=getsorted(pathspec)
if info<>"" then
	a=split(info,vbcrlf)
	wscript.echo split(a(0),";")(0) & vbcrlf & split(a(0),";")(1)
else
	wscript.echo "No such related info avail, either invalid path or empty folder."
end if

function getsorted(pathspec)
'sort parameter, order, etc hard coded, app specific

	const adVarChar=200 : const adDate=7
	on error resume next
	set ofolder=createobject("scripting.filesystemobject").getfolder(pathspec)
	if err.number<>0 then
		getsorted=""
		err.clear : set ofolder=nothing
		exit function
	end if
	on error goto 0

	set rs=createobject("ador.recordset")
	rs.fields.append "filename",adVarChar,255
	rs.fields.append "datecreated",adDate
	rs.open

	for each f in ofolder.files
		rs.addnew array("filename","datecreated"), array(f.name,f.datecreated)
		rs.update
	next

	info=""
	if not (rs.bof and rs.eof) then
		rs.sort = "datecreated desc"
		rs.movefirst
		do until rs.eof
			if rs.eof then
				info=info & rs("filename") & ";" &  rs("datecreated")
			else
				info=info & rs("filename") & ";" &  rs("datecreated") & vbcrlf
			end if
			rs.movenext
		loop
	end if

	rs.close
	set rs=nothing
	set ofolder=nothing : set fso=nothing

	getsorted=info

end function
regards - tsuji
 
'another way?

set oFolder=createobject("scripting.filesystemobject").getfolder("d:\test")
For Each aFile In oFolder.Files
If sNewest = "" Then
Set fNewest = aFile
sNewest = aFile.Name
Else
If fNewest.DateCreated < aFile.DateCreated Then
Set fNewest = aFile
End If
End If
Next

Msgbox fNewest.Name
 
'actually this looks better
Dim fNewest
set oFolder=createobject("scripting.filesystemobject").getfolder("d:\test")
For Each aFile In oFolder.Files
If fNewest = "" Then
Set fNewest = aFile
Else
If fNewest.DateCreated < aFile.DateCreated Then
Set fNewest = aFile
End If
End If
Next

Msgbox fNewest.Name
 
tsuji and mrmovie,

Outstanding.. Thank you both for your code and taking the time to respond.

Sometimes I just hit a wall and I really appreciate your posts.

Thank you,

Dougc
 
I converted the code that mrmovie provided to work under jscript as the backup script that I had already constructed is based on Jscript as opposed to VBScript. I went with mrmovies over yours tsuji was time to convert.. Although I have been with VB and VBScript, the other was a quicker port.

Thanks again to both of you for your input and direction. I would not have been able to get this done in such a short period of time without your help.

Have a great day..
Dougc



// identify the newest file in a folder
function findNewest(fldr) {
var fNewest="";
var fNwFile="";
var fso = new ActiveXObject("Scripting.FileSystemObject");
// Get Current Folder
var srcFolder = fso.GetFolder(fldr);
// Get Files in current directory
var files = new Enumerator(srcFolder.files);
// Loop through files
for(; !files.atEnd(); files.moveNext() ) {
if (fNewest == "") {
fNewest = Date.parse(files.item().DateLastModified);
fNwFile=files.item().name;
}
else {
if (fNewest < Date.parse(files.item().DateLastModified)) {
fNewest = Date.parse(files.item().DateLastModified);
fNwFile=files.item().name;
}
}
}
return fNwFile;
} // close function
 
thanks for the postive feedback Dougc, and for posting your finished code.
regards,
Richard
 
mrmovie was a nice and simple script, I only have one problem with it.

Code:
Option Explicit
'On Error Resume Next

Dim oFSO, oShell, oFolder, oFile ' as Objects
Dim sNewest, sFile, sFolder, sAccntShare, sLogFile ' as Strings

sFolder = "C:\Test"
sAccntShare = "K:\"
sLogFile = "SQL_Backup.log"

Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oFolder = oFSO.GetFolder(sFolder)

For Each sFile In oFolder.Files
    If sNewest = "" Then
        Set sNewest = sFile
    Else
        If sNewest.DateLastModified < sFile.DateLastModified Then
          Set sNewest = sFile
        End If
    End If
Next
WScript.Echo sNewest
'MsgBox "Copy " & sFolder & sNewest & " to " & sAccntShare
Call oFSO.CopyFile (sFolder & sNewest , sAccntShare, True)

The problem is that sNewest = C:\Test\TestDoc.txt, I know this is a stupid question, but how do I just get the TestDoc.txt without the C:\Test in front of it?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top