Finding File using ScriptingRuntime:   Return to List

Private Function FindFile(ByVal sFol As String, sFile As String, _
        nDirs As Integer, nFiles As Integer) As Long
' Initially nDirs & nFiles both come in as zero (0) -- however,
        these values are altered during recursion.

' sFol is the starting Folder (such as "C:\" for the path to search)
' sFile is the search string for the Filename.
On Error Resume Next
Dim MyFolder As Folder
Dim MyFile As File
Dim FileName As String
Dim f
If cmdSearch.Caption = "Canceled" Then' DoEvents not required with recursion
    Exit Function' A command button to stop the search
End If
Dim shit As String
Set fld = fso.GetFolder(sFol)
If optDirectories.Value = True Then
    If InStr(UCase(sFol), UCase(DirString)) > 0 Then' Both made uppercase
        NewPath = sFol
        If NewPath <> OldPath Then
            List1.AddItem sFol' Path placed in listbox
            OldPath = NewPath
            nFiles = nFiles + 1
        End If
    End If
Else
    If sFol <> "C:\System Volume Information" Then
        FileName = Dir(fso.BuildPath(fld.Path, sFile), vbNormal Or vbHidden Or vbSystem Or vbReadOnly)
        While Len(FileName) <> 0
            Set f = fso.GetFile(fso.BuildPath(fld.Path, FileName))
             ' check for files that are older than value in label
            shit = f.DateLastModified
            If Len(txtNumberofDays.Text) > 0 Then
                If f.DateLastModified > Now() - Val(txtNumberofDays.Text) Then
                    FindFile = FindFile + FileLen(fso.BuildPath(fld.Path, FileName))
                    nFiles = nFiles + 1
                    NewPath = fld.Path
                    If NewPath <> OldPath Then
                        List1.AddItem fld.Path' Load ListBox
                        OldPath = NewPath
                    End If
                End If
            Else
                FindFile = FindFile + FileLen(fso.BuildPath(fld.Path, FileName))
                nFiles = nFiles + 1
                NewPath = fld.Path
                If NewPath <> OldPath Then
                    List1.AddItem fld.Path' Load ListBox
                    OldPath = NewPath
                End If
            End If
            FileName = Dir() ' Get next file
            DoEvents
        Wend
    End If
End If
lblPath = "Searching " & vbCrLf & fld.Path
nDirs = nDirs + 1
lbldir.Caption = nDirs
If sFol <> "C:\System Volume Information" Then
    If fld.SubFolders.Count > 0 Then
        For Each MyFolder In fld.SubFolders
        ' below are directories you want to skip
            If MyFolder <> "C:\Recycler" Then
                DoEvents
                FindFile = FindFile + FindFile(MyFolder.Path, sFile, nDirs, nFiles)
            End If
        Next
    End If
End If
lblFiles.Caption = nFiles
End Function



Note to Webmaster