Check all files in all subfolders and obtain newest file

Check all files in all subfolders and obtain newest file

Post by Highlande » Wed, 16 May 2007 02:59:40


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
 
 
 

Check all files in all subfolders and obtain newest file

Post by Ayus » Wed, 16 May 2007 03:52:58

[Highlander]s message :


try this:

sFolder = "D:\Test"

Set objFSO = CreateObject("Scripting.FileSystemObject")

CheckSubFolders objFSO.getfolder(sFolder)

Dim vDT
Sub CheckSubFolders(Folder)
For Each Subfolder In Folder.SubFolders
For Each file In Sufolder.Files
WScript.Echo file.Path & vbTab & file.DateLastModified
IF file.DateLastModified > vDT Then
Set newfile = file
vdt = file.DateLastModified
End If
Next
CheckSubFolders(SubFolder)
Next
End Sub

Wscript.Echo newfile.path
IF DateDiff("n",newfile.DateCreated,Now) > 90 Then
Wscript.Echo "File is more than 90 minutes old."
END IF

Good Luck, Ayush.
--
Scripting- your first steps :
http://www.yqcomputer.com/

 
 
 

Check all files in all subfolders and obtain newest file

Post by Highlande » Thu, 17 May 2007 00:31:43


You did exactly what I was looking for - streamlined and removed some
redundancy from my script. I see now that I didn't need an array at
all.

However, your script as it was did not work. The line "Wscript.Echo
newfile.path" threw the following error:
Microsoft VBScript runtime error: Object required: 'newfile'

To address that error, and some other things, I've made some
modifications to your script and it now works great:

sFolder = "D:\Test"
Set objFSO = CreateObject("Scripting.FileSystemObject")

CheckSubFolders objFSO.getfolder(sFolder)

Dim fDate, NewestFile
Sub CheckSubFolders(Folder)
For Each Subfolder In Folder.SubFolders
For Each file In Subfolder.Files
WScript.Echo file.Path & vbTab & file.DateLastModified
IF file.DateLastModified => fDate Then
NewestFile = file
fDate = file.DateLastModified
End If
Next
CheckSubFolders(SubFolder)
Next
End Sub

Wscript.Echo vbCrlf & NewestFile
IF DateDiff("n",fDate,Now) > 90 Then
Wscript.Echo vbCrlf & "File is more than 90 minutes old."
END IF

Set objFSO = nothing


Thanks Ayush!

- Dave
 
 
 

Check all files in all subfolders and obtain newest file

Post by Highlande » Thu, 17 May 2007 05:42:05

n May 15, 10:31 am, Highlander < XXXX@XXXXX.COM > wrote:

There's one more issue that I have. My starting point is not actually
the folder "D:\Test". There are several subfolders within "D:\Test":

"D:\Test\One"
"D:\Test\Two"
"D:\Test\Three"
"D:\Test\Four"
"D:\Test\Five"

So my starting point - for obtaining a list of all files in all
subfolders - would be each one of those subfolders within "D:\Test". I
need a Loop or a "For Each..." statement. I've tried various
statements (For Each Subfolder In Folder.SubFolders, etc.) in the
script but I can't get it to work right.

Any suggestions that will get me over this hurdle would be greatly
appreciated. Thanks!

- Dave

 
 
 

Check all files in all subfolders and obtain newest file

Post by Ayus » Fri, 18 May 2007 02:32:02

[Highlander]s message :






sFolder = "D:\Test"
Set objFSO = CreateObject("Scripting.FileSystemObject")

CheckSubFolders objFSO.getfolder(sFolder)

Dim fDate, NewestFile
Sub CheckSubFolders(Folder)
For Each file In Subfolder.Files
WScript.Echo file.Path & vbTab & file.DateLastModified
IF file.DateLastModified => fDate Then
NewestFile = file
fDate = file.DateLastModified
End If
Next

For Each Subfolder In Folder.SubFolders
CheckSubFolders(SubFolder)
Next
End Sub

Wscript.Echo vbCrlf & NewestFile

IF DateDiff("n",fDate,Now) > 90 Then
Wscript.Echo vbCrlf & "File is more than 90 minutes old."
END IF


Good Luck, Ayush.
--
Scripting Solutions Center : http://www.yqcomputer.com/
 
 
 

Check all files in all subfolders and obtain newest file

Post by Ayus » Fri, 18 May 2007 02:33:37

[Highlander]s message :


This should work:
sFolder = "D:\Test"

Set objFSO = CreateObject("Scripting.FileSystemObject")

CheckSubFolders objFSO.getfolder(sFolder)

Dim vDT,newfile ' <<<<
Sub CheckSubFolders(Folder)
For Each Subfolder In Folder.SubFolders
For Each file In Sufolder.Files
WScript.Echo file.Path & vbTab & file.DateLastModified
IF file.DateLastModified > vDT Then
Set newfile = file
vdt = file.DateLastModified
End If
Next
CheckSubFolders(SubFolder)
Next
End Sub

Wscript.Echo newfile.path
IF DateDiff("n",newfile.DateCreated,Now) > 90 Then
Wscript.Echo "File is more than 90 minutes old."
END IF


Good Luck, Ayush.
--
Script Center-Script Repository : http://www.yqcomputer.com/