如何FINDSTR如果无法找到字符串有条件地采取行动 [英] How to conditionally take action if FINDSTR fails to find a string
问题描述
我有一个批处理文件如下:
I have a batch file as follows;
CD C:\MyFolder
findstr /c:"stringToCheck" fileToCheck.bat
IF NOT XCOPY "C:\OtherFolder\fileToCheck.bat" "C:\MyFolder" /s /y
我得到一个错误(C:\\ OtherFolder \\ fileToCheck.bat。在这个时候意外)试图执行这个时候
I am getting an error ("C:\OtherFolder\fileToCheck.bat" was unexpected at this time.) when trying to execute this.
请让我知道我做错了。
推荐答案
您没有评估为IF的条件。我猜你想,如果你在fileToCheck找到stringToCheck不复制。你需要做的是这样(code未经测试,但你的想法):
You are not evaluating a condition for the IF. I am guessing you want to not copy if you find stringToCheck in fileToCheck. You need to do something like (code untested but you get the idea):
CD C:\MyFolder
findstr /c:"stringToCheck" fileToCheck.bat
IF NOT ERRORLEVEL 0 XCOPY "C:\OtherFolder\fileToCheck.bat" "C:\MyFolder" /s /y
编辑由dbenham 的结果
的以上测试是错误的,它始终计算为FALSE。结果
正确的测试是 IF ERRORLEVEL 1 XCOPY ...
的
更新:我不能测试code,但我不知道,如果它没有找到任何事情的返回值FINDSTR实际上返回。您可能必须做一些事情,如:
Update: I can't test the code, but I am not sure what return value findstr actually returns if it doesn't find anything. You might have to do something like:
CD C:\MyFolder
findstr /c:"stringToCheck" fileToCheck.bat > tempfindoutput.txt
set /p FINDOUTPUT= < tempfindoutput.txt
IF "%FINDOUTPUT%"=="" XCOPY "C:\OtherFolder\fileToCheck.bat" "C:\MyFolder" /s /y
del tempfindoutput.txt
这篇关于如何FINDSTR如果无法找到字符串有条件地采取行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!