Hello all.
What I'm trying to do is the following:
1. Get a list of all files in all subfolders from a starting point
(D:\Test).
2. Get the newest file from this list.
3. Indicate whether this newest file is more than 90 minutes old.
Searching through previous posts, I've used elements of 3 different
scripts to put a new script
together:
'~~~ Create Dynamic Array for Filenames
Dim arrFiles(), i
Redim arrFiles(-1)
sFolder = "D:\Test"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(sFolder)
Set colFiles = objFolder.Files
CheckSubFolders(objFolder)
Sub CheckSubFolders(objFolder)
Set colFolders = objFolder.SubFolders
For Each objSubFolder In colFolders
Set colFiles = objSubFolder.Files
'~~ Populate the array with filenames from each SubFolder
For Each objFile In colFiles
Redim Preserve arrFiles(UBound(arrFiles) + 1)
arrFiles(UBound(arrFiles)) = objFile.Path
Next
'~~ Repeat subroutine until all SubFolders have been checked
CheckSubFolders(objSubFolder)
Next
End Sub
'~~~ From the Array of Filenames get the newest file
Dim vDT, vFName, NewestFile
For i = 0 To UBound(arrFiles)
Set oFile = objFSO.GetFile(arrFiles(i))
WScript.Echo oFile.Path & vbTab & oFile.DateLastModified
IF oFile.DateLastModified > vDT Then
vFName = oFile.Path
vDT = oFile.DateLastModified
End If
Next
NewestFile = vFName
Wscript.Echo vbCrlf & NewestFile & vbCrlf
'~~~ Check if newest file is more than 90 minutes old
Set oFile = objFSO.GetFile(NewestFile)
IF DateDiff("n",oFile.DateCreated,Now) > 90 Then
Wscript.Echo "File is more than 90 minutes old."
END IF
Set objFSO = nothing
Set objFolder = nothing
Set colfiles = nothing
Set colFolders = nothing
Set oFile = nothing
My script works fine, but I'm guessing it could probably be
streamlined and some redundancy
removed. Or, maybe there's a completely different and better method
that can be used.
Any suggestions would be greatly appreciated. Thanks.
- Dave