2009-03-06 21 views
0

在windows失敗CMD腳本殼:當文本被發現

echo "foo bar" | find "foo"

成功(即錯誤級別= 0)。但是,我想要一個失敗的腳本(即錯誤級別<> 0),當它在某些輸入文本中找到某個單詞時。有任何想法嗎?

回答

1

一個技巧可以模擬你的願望。只有前三行是必需的,其餘只是一個測試。

c:> echo "foo bar" | find "foo" 
c:> if x%errorlevel%==x0 echo 1 | find "2" 
c:> if not x%errorlevel%==x0 echo 1 | find "1" >nul 2>nul 
c:> echo %errorlevel% 
1