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

Find newest files in a series of folders and output them to a file

Status
Not open for further replies.

budmor

MIS
Nov 9, 1999
16
0
0
CH
I have a series of folders that are backed up every night using xcopy. I want to be able to run a script to report the newest filename and DateLastModified of the files in each of a number of folders. I have found an example of a script that shows me the newest file and its date/timestamp but I want to iterate through a number of folders and subfolders and report each newest entry into a log file.

Below is the example for checking the newest file in r:\server1\oracle\instance1\Archive. I also want to get teh newest file for ..\instance1\Rman and ..\instance1\Folder3 etc.

set oFolder=createobject("scripting.filesystemobject").getfolder("r:\server1\oracle\instance1\Archive")
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 & " " & fNewest.DateLastModified

I guess the output of the file should look like:

r:\server1\oracle\instance1\Archive\fileabc.dat 06/04/2010 01:01:27
r:\server1\oracle\instance1\Rman\file123.dat 06/04/2010 01:05:27
r:\server1\oracle\instance2\Archive\ffff.dat 06/04/2010 02:01:27

Any help would be greatly appreciated.
 
I've written a script precisely for this purpose.

Code:
CONST strFolder = "C:\temp\"

set objFSO = CreateObject("Scripting.FileSystemObject")
set objOutput = objFSO.OpenTextFile("C:\temp\output.txt", 2, true)
set objShell = CreateObject("WScript.Shell")

function traverseDir(strDir, boolSubDirs)
	on error resume next
	set objFolder = objFSO.GetFolder(strDir)
	
	for each objSubFolder in objFolder.SubFolders
		traverseDir objSubFolder.Path, boolSubDirs
		if (boolSubDirs) then strContent = strContent & strResults
	next
	
	for each objFile in objFolder.Files
		if (objFile.DateCreated > objRecent.DateCreated) then
			set objRecent = objFile
		end if
	next
    objOutput.WriteLine objRecent.Path & vbTab & objRecent.DateLastModified
end function

traverseDir strFolder, true

objOutput.close
objShell.Run("C:\temp\output.txt")

the on error resume next is generally bad practice but is useful in this case

-Geates
 
Hi Geates, this is exactly what I was looking for. Thanks very much for your excellent reply!

Mickey
 
Discussion Post:

Take notice that the script is really a simple folder traversal function with a single conditional statement

Code:
if (objFile.DateCreated > objRecent.DateCreated) then  
   set objRecent = objFile
end if

Therefore, the statement can be changed to reflect some other type of condition - search for file extensions, check file sizes, manipulate file content, etc.

Budmor, you were SO close to writting the script you needed. It simply lacked the recursion needed to traverse nested containers.

-Geates
 
Exactly, I modified it slightly to evaluate the DateLastModified instead of the DateCreated. One other thing I was considering was to try to pass in a text file containing a list of folders (with their full path) rather than just hardcode in the constant as above. I guess this would involve opening the FolderList file for reading into objStartFolder? Please forgive my lack of skill in this area! Thanks again for your help. Mickey
 
Because a file of paths would provide a static list of files, there is no need for recursion. Simply open the file and apply each entry to the condition.

Code:
set objFSO = CreateObject("Scripting.FileSystemObject")
set objOutput = objFSO.OpenTextFile("C:\temp\output.txt", 2, true)
set objInput = objFSO.OpenTextFile("C:\temp\input.txt", 1)

do while NOT objInput.AtEndOfStream
   strLine = objInput.ReadLine
   set objFile = objFSO.GetFile(strLine)
   if (this = that) then
      'do stuff
   end if
loop

objOutput.close
objInput.close

-Geates
 
Oh, if you want to pass a file as an arugment using the command line or drag and drop (GUI), read the argument:

Code:
set objShell = Wscript.CreateObject("WScript.Shell")
set objArgs = objShell.Arguments
set objInput = objFSO.GetFile([red]objArgs(0)[/red])

-Geates
 
Thanks Geates, I will try to incorporate this logic into the script. Regards, Mickey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top