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?

有帮助吗?

解决方案

Just redirect the output to nul.

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

其他提示

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!)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top