Hello,
This sample will loop through a folder looking for a folder or file that contains the word. When it finds the word it will give the location of the file or folder. The example searches through the whole C:\ folder.
Private Sub LoopThroughFolder(ByVal Folder As String, ByVal WordToSearchFor As String)
Dim SubDirectories As String() = System.IO.Directory.GetDirectories(Folder)
Dim Files As String() = System.IO.Directory.GetFiles(Folder)
For i = 0 To SubDirectories.Length - 1
If SubDirectories(i).Contains(WordToSearchFor) Then
MessageBox.Show("Found the string: " & WordToSearchFor & ", at the folder: " & SubDirectories(i))
Exit Sub
End If
LoopThroughFolder(SubDirectories(i), WordToSearchFor)
Next
For i = 0 To Files.Length - 1
If Files(i).Contains(WordToSearchFor) Then
MessageBox.Show("Found the string: " & WordToSearchFor & ", at the file: " & Files(i))
Exit Sub
End If
Next
End Sub
If you want to search for a different word simply change this:
Dim SearchWord As String = "secret"
LoopThroughFolder("C:\", SearchWord)
To:
Dim SearchWord As String = "The word you want to search for"
LoopThroughFolder("C:\", SearchWord)
I hope it helps.
Hi @Ryan Lashway ,
Did you set 'searchSubFolders' parameter to 'True'?
The code works for me, could you share more details here?
It searches the first subfolder but if there are nested subfolders below that level it isnt searching those.