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

Re: Check all files in all subfolders and obtain newest file by Ayush

Ayush
Mon May 14 13:52:58 CDT 2007

[Highlander]s message :
> 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.

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.microsoft.com/technet/scriptcenter/topics/beginner/firststeps.mspx

Re: Check all files in all subfolders and obtain newest file by Highlander

Highlander
Tue May 15 10:31:43 CDT 2007

On May 14, 1:52 pm, Ayush <"ayushmaan.j[aatt]gmail.com"> wrote:
> [Highlander]s message :
>
> > 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.
>
> 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.microsoft.com/technet/scriptcenter/topics/beginner/firstst...

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


Re: Check all files in all subfolders and obtain newest file by Highlander

Highlander
Tue May 15 15:42:05 CDT 2007

On May 15, 10:31 am, Highlander <tron9...@msn.com> wrote:
> On May 14, 1:52 pm, Ayush <"ayushmaan.j[aatt]gmail.com"> wrote:
>
>
>
>
>
> > [Highlander]s message :
>
> > > 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.
>
> > 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.microsoft.com/technet/scriptcenter/topics/beginner/firstst...
>
> 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- Hide quoted text -
>
> - Show quoted text -

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


Re: Check all files in all subfolders and obtain newest file by Ayush

Ayush
Wed May 16 12:32:02 CDT 2007

[Highlander]s message :
> 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!



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://snipurl.com/Scripting_Solutions

Re: Check all files in all subfolders and obtain newest file by Ayush

Ayush
Wed May 16 12:33:37 CDT 2007

[Highlander]s message :

> 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'

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://snipurl.com/Script_Repository