INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Find files in folders and subfolders

Find files in folders and subfolders

(OP)
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

 

RE: Find files in folders and subfolders

(OP)
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.  

RE: Find files in folders and subfolders

(OP)

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



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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close