Search for string within filename in subdirectories

Ikarian picture Ikarian · Apr 25, 2013 · Viewed 27.5k times · Source

I have a large directory of folders (call it C:\Main). I need to set up a batch script to search the subfolders of that directory for a string within the filename (not the text within the file). I'm having trouble finding an answer.

Essentially, let's say I need to search for the string "abcd" within all the filenames in C:\Main\*. I'm only looking for matches that are a XML file. So I need to find:

C:\Main\Secondary1\abcd_othertext.xml

C:\Main\Secondary2\abcd_othertext.xml

C:\Main\Secondary3\abcd_othertext.xml

among all the hundreds of folders in that Main directory. Then I need to output all matches (ideally to individual variables in a bat file, but that's a different can of worms). Thanks in advance for your help.

Answer

Tall Software Dude picture Tall Software Dude · Apr 25, 2013

The DIR command can perform a wildcard search in subdirectories.

DIR abcd*.xml /s /b