How to scan all files in a directory and subdirectories for a string or variable name.
Okay so I needed to generate a list of files on my PC that contain the variable $license[xxxxx]. Whilst I knew I could write a PHP script to do this, I thought it’d be much quicker to let my PC do all the work using findstr.
First, let’s explain what FINDSTR is and how it works.
Findstr is a windows command you can use in the Windows Command Prompt (cmd / cmd.exe) to find out if a string exists in a defined file or in this case, list off all files containing a string.
So I came up with this:
Just to explain what each of the toggles I used mean:
/m = Only display the filename of each file found matching the search. (without this, it will quote the entire line and line number the string was found on)
/s = Includes all subdirectories as well as the defined directory
/i = Forces the search to be case insensitive.
This worked, but it also spat off reams of results I couldn’t do anything with.. Also, there were so many results, it exceeded the command prompt capacity and trimmed off about half of the results. So, you want to dump the results to a .txt file, which will actually leave you with usable results.
To do this, you simply add > C:/path/to/result-file.txt (this file shouldn’t exist already! It will be created!)
So I ended up with this:
1 | C:\Windows\system32>findstr /m /s /i "$license" C:\dev\www\* > C:\license-results.txt |
Which worked perfectly! I know I usually don’t blog about windows stuff, but I thought this would be very useful to my fellow webmasters
You’re welcome!
Popularity: 6%
