I've found a number of solutions to this problem, but unfortunately none of them seem to work on my windows box.
I'm running a
FOR loop in a batch file to loop through a number of text files. For each text file, I want to
ECHO its name (and possibly redirect output to file) if a
grep (from WinGNU) returns a line. A quick google search shows me to do it this way:
IF grep -c pattern file ( stuff )
However this throws an error
-c was unexpected at this time.
Alternatively, I figured I could use the exit code from
grep --quiet like so:
grep -q pattern file IF ERRORLEVEL 1 ( stuff )
This always fails the IF test, even when the grep succeeds.
Is there a good way to run a conditional statement in Windows if my grep finds a match?
My code is:
SET locations=( a, long, list, of, last, two, octets ) SETLOCAL ENABLEDELAYEDEXPANSION FOR %%r IN %locations% DO ( grep -i "^\[CardSwipe\]" \\10.0.%%r\path\to\file.ini IF ERRORLEVEL 1 echo %%r
My output is:
[this line left intentionally blank] :)