我正在尝试将find
命令与if..else
语句一起使用。我真正想要做的是在多个日志文件中搜索特定的字符串。如果找到该字符串,则要将该文件的名称附加“已通过”,否则将“失败”附加到另一个文本文件中。
我尝试了以下操作,但不起作用:
if (find /n /i "0 error" "filename.log") (
echo "Passed" > log.txt
) else (
echo "Failed" > log.txt
)
答案 0 :(得分:1)
CMD /批处理不支持if
语句中的命令。运行命令后,您需要评估错误级别(命令的退出代码):
set "logfile=filename.log"
find /n /i "0 error" "%logfile%" >nul
if %errorlevel% equ 0 (
>>log.txt echo %logfile% - Passed
) else (
>>log.txt echo %logfile% - Failed
)
答案 1 :(得分:0)
If I understand your question correctly:
To get a .txt
file containing only the names of the logs in the current directory which contained 0 error
:
FindStr /MIC:"0 Error" *.log>"Passed.txt"
If you wanted a log.txt
to prodvide feedback on each log file in the current directory, instead…
From the Command Prompt:
(For %A In (*.log) Do @FindStr /MIC:"0 Error" "%A">Nul&&(@Echo %A Passed)||@Echo %A Failed)>"log.txt"
From a batch file:
@Echo Off
(For %%A In (*.log) Do FindStr /MIC:"0 Error" "%%A">Nul&&(
Echo %%A Passed)||Echo %%A Failed)>"log.txt"