chrisandsally
chrisandsally

Reputation: 11

Search Folders and Subfolders for filename containing the words "data" and message box its path

I have A folder on a drive that I require to search (including subfolders) and show in a message box all files that contain a filename of "data" (for example C:\data\test-data.txt).

Set fso = CreateObject("Scripting.FileSystemObject")

CopyUpdater fso.GetFolder("c:\data\")

Sub CopyUpdater(fldr)
  For Each f In fldr.Files
    If LCase(f.Name) = "data" Then
      WScript.Echo objFile.Name
    End If
  Next

  For Each sf In fldr.SubFolders
    CopyUpdater sf
  Next
End Sub

Upvotes: 1

Views: 2555

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200533

If you want partial matches on the filename use InStr instead of doing a direct comparison. Also, for displaying the path instead of the name you need to echo the Path property. And of course you need to use the the correct variable.

Sub CopyUpdater(fldr)
  For Each f In fldr.Files
    If InStr(LCase(f.Name), "data") > 0 Then
      WScript.Echo f.Path
    End If
  Next

  For Each sf In fldr.SubFolders
    CopyUpdater sf
  Next
End Sub

Upvotes: 3

Related Questions