我只需要一个非常简单的东西,例如“运行此命令,如果控制台输出中某处有'this string',则成功,否则,则失败”。有这样的工具吗?
最佳答案
并不是我所知道的,但是您可以轻松地在另一个批处理脚本中编写一个脚本。
call TestBatchScript.cmd > console_output.txt
findstr /C:"this string" console_output.txt
如果找到该字符串,会将%errorlevel%设置为零;如果不存在该字符串,则将其设置为非零。然后,您可以使用
IF ERRORLEVEL 1 goto :fail
进行测试,并在:fail
标签之后执行所需的任何代码。如果要对几个这样的字符串进行紧凑的求值,可以使用||。句法:
call TestBatchScript.cmd > console_output.txt
findstr /C:"teststring1" console_output.txt || goto :fail
findstr /C:"teststring2" console_output.txt || goto :fail
findstr /C:"teststring3" console_output.txt || goto :fail
findstr /C:"teststring4" console_output.txt || goto :fail
goto :eof
:fail
echo You Suck!
goto :eof
或者,您甚至可以更进一步,从文件中读取字符串列表
call TestBatchScript.cmd > console_output.txt
set success=1
for /f "tokens=*" %%a in (teststrings.txt) do findstr /C:"%%a" console_output.txt || call :fail %%a
if %success% NEQ 1 echo You Suck!
goto :eof
:fail
echo Didn't find string "%*"
set success=0
goto :eof