Problem
What is the eqivalent of grep in Windows.
Solution
1) Windows XP onwards come with the DOS command FINDSTR
Example :
I want to search the Microsoft .NET Chart Controls Sample for a particular string and list only the filename.
FINDSTR /S /C:"This sample demonstrates Stacked Area" /m *.*
/S recursively searches sub-directories.
/C: sets the string you are searching for.
/M lists filename only.
For more help type FINDSTR -? in DOS
Output from the command :
C:\Inetpub\wwwroot\WebSamples>FINDSTR /S /C:"This sample demonstrates Stacked Area" /m *.*
ChartTypes\BarColumnCharts\Stacked\stackedchart.aspx
2) Another way to search is using Windows PowerShell. This example will search for the same string as the FINDSTR example and list only the filenames containing that string.
get-childitem ./* -include *.* -recurse -exclude msc_cntr*.txt |select-string -pattern "This sample demonstrates Stacked Area"-casesensitive | select-object Path
Rough guide what is happening here :
a) get-childitem ./* -include *.* -recurse -exclude msc_cntr*.txt - This will get the recursive search for all files from you current directory down. -exclude msc_cntr*.txt was used as I the file was open and locked at the time, add error output I did not require.
b) select-string -pattern "This sample demonstrates Stacked Area"-casesensitive - This is just case sensitive search on the string "This sample demonstrates Stacked Area".
c) select-object Path - This displays the filename and full file path, for the filename with no path use "select-object Filename".
Output from the command :
PS C:\Inetpub\wwwroot\WebSamples> get-childitem ./* -include *.* -recurse -exclude msc_cntr*.txt | select-string -pattern "This sample demonstrates Stacked Area" -casesensitive | select-object Path
Path
----
C:\Inetpub\wwwroot\WebSamples\ChartTypes\BarColumnCharts\Stacked\stackedchart.aspx