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

Find files in folders and subfolders

Status
Not open for further replies.

lbradio

IS-IT--Management
Apr 17, 2010
3
NL
I'm working on a script which have to check weather a file is younger than 2 hours. It should check in all folders and subfolders.

After finding a file younger than 2 hours, an email should be sent.

The script is working ok for the given folder, but I also need to check subfolders. And that's where I'm stuck.

I need some help in this one.

==================================

sPath = "c:\test"
'sPath = "c:\2"



Set oFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")

Set oFolder = oFSO.GetFolder(sPath)
Set oFiles = oFolder.Files


If oFiles.Count > 0 Then

bolFileIsNewEnough = False ' init value


For Each oFile In oFiles
On Error Resume Next
dFileModDate = oFile.DateLastModified
If Err.Number = 0 Then
If DateDiff("n", dFileModDate, Now) < 120 Then
bolFileIsNewEnough = True
msgbox "Found file younger than 2 hours"

objShell.Run "C:\postie.exe -host:172.xx.xx.xx:26 -from:server@domain.com -to:me@domain.com -s:subject -msg:The_message "


Exit For
End If
End If
Next
On Error Goto 0

If Not bolFileIsNewEnough Then
MsgBox "All files are older than 2 hours."
End if

Else
MsgBox "Directory is empty"
End If

 
Thanks for the reply. I've read your suggestion. I've tried some simular solutions, but until now without the result. I'll give it a try again.

Thanks.
 

I've managed to insert the recursive routine. Even filtered on extension
Now there's only one chalenge to go.

I would like to search for files in folders and subfolders, but exclude a specific subfolder.

Anyone a suggestion?






============================================================================================



' Check wether a file exist in folders and subfolders, older than x hours.
' If a file is found, an email will be sent.

' L. de Bles 2010




sPath = "c:\test" ' Path to start whit
Stime = 120 ' Number of minutes to compare with
Const strSearchedExt = "icm" 'extension without the dot


Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sPath)
Set oFiles = oFolder.Files
Set objShell = CreateObject("Wscript.Shell")


' Wscript.Echo oFolder.Path
bolFileIsNewEnough = False ' init value

For Each oFile in oFiles
On Error Resume Next

strExt = oFSO.GetExtensionName (oFile.Name)
if 0 = StrComp(strExt, strSearchedExt, vbTextCompare) Then
dFileModDate = oFile.DateLastModified

If Err.Number = 0 Then

If DateDiff("n", dFileModDate, Now) > sTime Then
bolFileIsNewEnough = True
Wscript.Echo oFolder.Path & "\" & oFile.Name
' msgbox "bestand gevonden"
' objShell.Run "C:\Beheer\postie.exe -host:<mailserver> -from:server@test.com -to:me@test.com -s:subject -msg:message "
End if

End if

End if

Next










ShowSubfolders oFSO.GetFolder(sPath)

Sub ShowSubFolders(Folder)

For Each Subfolder in Folder.SubFolders
Set oFolder = oFSO.GetFolder(Subfolder.Path)
Set oFiles = oFolder.Files

For Each objFile in oFiles
On Error Resume Next
strExt = oFSO.GetExtensionName (objFile.Name)

if 0 = StrComp(strExt, strSearchedExt, vbTextCompare) Then
Wscript.Echo oFolder.Path & "\" & objFile.Name
dFileModDate = objFile.DateLastModified

If Err.Number = 0 Then

If DateDiff("n", dFileModDate, Now) > sTime Then
bolFileIsNewEnough = True
Wscript.Echo oFolder.Path & "\" & objFile.Name
' msgbox "bestand in subfolder gevonden"
' objShell.Run "C:\Beheer\postie.exe -host:<mailserver> -from:server@test.com -to:me@test.com -s:subject -msg:message "
End if

End if

End if

Next

ShowSubFolders Subfolder

Next

End Sub



============================================================================================
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top