Question

I'm new to windows batch scripts but have started using the findstr command e.g.

findstr "test" file.txt
if not errorlevel 1 ( echo Found it!)

The code manages to findtest in file.txt but I don't want it to output the line where it finds "test" I just want it to echo Found it!

Is this possible with findstr or should I be using something else?

Était-ce utile?

La solution

Just redirect the output to nul.

findstr "test" file.txt >nul
if not errorlevel 1 ( echo Found it!)

Autres conseils

Bali C got the redirection correct, but the OP's original logic was incorrect. Any of the following will work.

findstr "test" file.txt >nul
if not errorlevel 1 (echo Found it!)

or

findstr "test" file.txt >nul
if %errorlevel%==0 (echo Found it!)

or my personal favorite

findstr "test" file.txt >nul && (echo Found it!)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top