FindStr.exe an egrep-like tool

I've long thought of writing a little tool like grep (or egrep) which would allow me to quickly use regex to search for text within files. This weekend while reading an article about Standard I/O and Console Applications I discovered the Windows command-line tool FindStr.

FindStr.exe is a command-line tool which searches for patterns of text in files using regular expressions. Let's say I've got a folder full of documents and I want to search through and find any instance of the pattern "WebService" I can fire up a Command Prompt, change the path to my where my folder is and use FindStr.exe to perform the search:

cd c:\MyFolder
findstr /s /i WebService *.*

 

This displays a list of all lines in all files in which the search term was found. Another neat trick is that you can use the greater than operator - ">" - to redirect the results to a text file, like so:

cd c:\MyFolder
findstr /s /i WebService *.* >Results.txt

 

This redirects all results to a file named Results.txt. So there it is, an egrep-like utility already which is already baked into Windows OS - I never knew that!

Cross posted on http://weblogs.asp.net/DNeimke and http://blogs.regexadvice.com/DNeimke

12 Comments

  • There's also find.exe. It doesn't support regular expressions but works with unicode files (findstr doesn't).


  • Thanks Pavel, I did notice that yesterday. There's a nice Help Topic titled "Using filters" which shows how to use 3 handy command-line tools:



    - More

    - Find

    - Sort



    I'm very new to the command-line world but, I'm very impressed with things like this:



    C:\ find "Jones" maillst.txt | sort >Results.txt



    That command will find lines that contain the text "Jones", sort all of the lines that it finds then output the results to a text file. Cool eh? :)



    There's another useful Help Topic titled "Command-line reference A-Z" which lists all of the Windows XP tools.



  • I always use it like this: (recursive search, in this case .cs files)



    dir /s /b *.cs | findstr /f:/ "foo"



    :)

  • Thanks Christophe and Frans... very useful tips!

  • A very good tool,I like it very much.

  • Can findstr be used accross mapped drives?

  • Frans Bouma wrote:

    "I always use it like this: (recursive search, in this case .cs files)

    dir /s /b *.cs | findstr /f:/ "foo"
    "

    Frans's solution of compounding two commands together to search for "foo" recursively through subfolders in files ending with .cs seems unneccesarily complex. Why not use the equivalent functionality built-in to findstr? Here's the equivalent using only the findstr command:

    findstr /s "foo" *.cs

    The "/s" argument tells findstr to search recursively through all subfolders of the current directory.
    The "*.cs" argument tells findstr to only look in files ending with .cs

  • Just found out about this command for the first time but the problem I have is that it does not support regular expressions searches at all.

    It ONLY supports using the regular expression wildcards that are listed when you do findstr /? like so:

    Regular expression quick reference:
    . Wildcard: any character
    * Repeat: zero or more occurrences of previous character or class
    ^ Line position: beginning of line
    $ Line position: end of line
    [class] Character class: any one character in set
    [^class] Inverse class: any one character not in set
    [x-y] Range: any characters within the specified range
    \x Escape: literal use of metacharacter x
    \ Word position: end of word

    It DOES NOT support any other type of regular expression syntax such as "a+", "a{2}", "a{2,5}", "abc|xyz" or even special characters such as \n, \t, etc

  • как всегда на высоте

  • 81675.. Reposted it :)

  • 50 % discounts on all airtickets flights and hotels as well as all football matches
    email us at makitelove@yahoo.com

    no advance payment needed

  • I don't even know the way I ended up here, however I believed this publish used to be great. I do not recognize who you are but certainly you are going to a famous blogger in case you aren't already.
    Cheers!

Comments have been disabled for this content.