我是Windows批處理腳本的新手,但已經開始使用findstr
命令,例如,Windows批處理文件查找字符串,但不打印
findstr "test" file.txt
if not errorlevel 1 (echo Found it!)
代碼設法找到file.txt
test
但我不希望它輸出,其中發現「測試」我只是希望它呼應Found it!
行這可能與findstr
或應我正在使用別的東西?
我是Windows批處理腳本的新手,但已經開始使用findstr
命令,例如,Windows批處理文件查找字符串,但不打印
findstr "test" file.txt
if not errorlevel 1 (echo Found it!)
代碼設法找到file.txt
test
但我不希望它輸出,其中發現「測試」我只是希望它呼應Found it!
行這可能與findstr
或應我正在使用別的東西?
只是將輸出重定向到nul
。
findstr "test" file.txt >nul
if not errorlevel 1 (echo Found it!)
編輯 - 我修正了錯誤級別的邏輯來匹配編輯的問題。 – dbenham
@dbenham謝謝! –
巴厘島C重定向正確,但OP的原始邏輯不正確。以下任何一項都可以使用。
findstr "test" file.txt >nul
if not errorlevel 1 (echo Found it!)
或
findstr "test" file.txt >nul
if %errorlevel%==0 (echo Found it!)
或我個人最喜歡
findstr "test" file.txt >nul && (echo Found it!)
在你的問題中的代碼不能給你結果狀態。它應該閱讀'如果不是errorlevel 1(回聲發現它!)' – dbenham
對不起,我只是記下了一個簡單的例子。我現在修復了它。 – Alex