Slid3r
Slid3r

Reputation: 323

VBS to Search for Multiple Files Recursively in C:\Users

I need to recursively search for multiple files through the C:\Users directory tree recursively.

If I find any of the specified files in any of the sub-directories, I want to echo out the full path.

Here is what I have:

Dim fso,folder,files,sFolder,newFolder  
Dim arr1  
arr1 = Array("myFile1.pdf","myFile2.pdf","myFile3.pdf","nutbag.rtf","whoa.txt")  
  Set fso = CreateObject("Scripting.FileSystemObject")  
  sFolder = "C:\Users"  
  Set folder = fso.GetFolder(sFolder)  
  Set files = folder.SubFolders  
  For each folderIdx In files  
    IF (Instr(folderIdx.Name,"Default") <> 1) Then  
     If (Instr(folderIdx.Name,"All Users") <> 1) Then  
      newFolder = sfolder & "\" & folderIdx.Name  
      CopyUpdater fso.GetFolder(newFolder)  
     End If  
    End If  
  Next  
Sub CopyUpdater(fldr)  
 For Each f In fldr.Files  
  For Each i in arr1  
    If LCase(f.Name) = i Then  
       WScript.echo(f.name)  
    End If  
  Next  
Next  
For Each sf In fldr.SubFolders  
    CopyUpdater sf  
  Next  
End Sub

If I run it as 'Administrator', I get:

VBScript runtime error: Permission Denied

If I run it as 'Local System' user, I get:

VBScript runtime error: Path not found

If I add, 'On Error Resume Next' to the beginning to suppress the errors, I get nothing back.

I have placed a text file called 'whoa.txt' in numerous locations around the C:\Users sub-dirs.

My suspicion is that it is a Windows permissions thing, but I am unsure.

Thanks much.

Upvotes: 0

Views: 2690

Answers (1)

PatricK
PatricK

Reputation: 6433

First I didn't use your code, it confuses me what you are trying to accomplish.

Next you should run the script in Administrator mode command prompt. This should allow you to check if the file is there.
cmd

Then paste code below to a vbs file and cscript it. This code displays all the matched filenames.
My idea is that instead of going through all files in any folder for a matching filename, check if those wanted files exists in that folder - this is generally faster as some folders contains hundreds of files if not thousands (check your Temp folder!).

Option Explicit

Const sRootFolder = "C:\Users"
Dim fso
Dim arr1
Dim oDict ' Key: Full filename, Item: Filename

Main

Sub Main
    arr1 = Array("myFile1.pdf", "myFile2.pdf", "myFile3.pdf", "nutbag.rtf", "whoa.txt")
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set oDict = CreateObject("Scripting.Dictionary")
    ' Call Recursive Sub
    FindWantedFiles(sRootFolder)
    ' Display All Findings from Dictionary object
    DisplayFindings
    Set fso = Nothing
    Set oDict = Nothing
End Sub

Sub FindWantedFiles(sFolder)
    On Error Resume Next
    Dim oFDR, oItem
    ' Check if wanted files are in this folder
    For Each oItem In arr1
        If fso.FileExists(sFolder & "\" & oItem) Then
            oDict.Add sFolder & "\" & oItem, oItem
        End If
    Next
    ' Recurse into it's sub folders
    For Each oFDR In fso.GetFolder(sFolder).SubFolders
        FindWantedFiles oFDR.Path
    Next
End Sub

Sub DisplayFindings()
    Dim oKeys, oKey
    oKeys = oDict.Keys
    For Each oKey In oKeys
        wscript.echo oKey
    Next
End Sub

Upvotes: 2

Related Questions