I once had a version of grep on my PC (I think it came with an early version of Delphi) which supported searching in nested folders with the -r switch. Now something (I suspect a later version of Delphi) has hijacked the old grep and replaced it with something that announces itself thus:
C:\PROJECTS\>grep --version
grep (GNU grep) 2.4.2
Copyright 1988, 1992-1999, 2000 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
it provides these options for recursion:
-d, --directories=ACTION how to handle directories
ACTION is 'read', 'recurse', or 'skip'.
-r, --recursive equivalent to --directories=recurse.
but I can't make it return results from files within folders, so grep -r fred *.txt will only find files in the current folder containing "fred", and files within subfolders are ignored.
What's the magic option required here?
ANSWER from Rich Homolka is below, but here's my batch wrapper developed from that answer:
:==========================================================================
: GrepExt - searches files matching an extension recursively for a string
:
: Usage : call GrepExt <regex string to search for> <extension>
: Example : call GrepExt "procedure\ *Add" pas
:
: Notes : - quotes only needed if regex contains spaces
: - remove the first "-i" for case-sensitive search
: - the second "-i" ensures the case of the extension doesn't matter
: - ErrorLevel set to 0 if there were any matches, 1 otherwise
: - this is very inefficient when the folders contain a large
: number of files that don't match the extension, as *all* files
: are grepped and the results are then grepped to filter out
: the hits from files that don't match the extension.
:
@echo off
grep -i -r "%~1" . | grep -i "^[^:]*\.%~2:"