如果 FINDSTR 找不到字符串,如何有条件地采取行动 [英] How to conditionally take action if FINDSTR fails to find a string

查看:31
本文介绍了如果 FINDSTR 找不到字符串,如何有条件地采取行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的批处理文件;

I have a batch file as follows;

CD C:MyFolder
findstr /c:"stringToCheck" fileToCheck.bat
IF NOT XCOPY "C:OtherFolderfileToCheck.bat" "C:MyFolder" /s /y

我在尝试执行此操作时收到错误(此时C:OtherFolderfileToCheck.bat"是意外的.).

I am getting an error ("C:OtherFolderfileToCheck.bat" was unexpected at this time.) when trying to execute this.

请告诉我我做错了什么.

Please let me know what I am doing wrong.

推荐答案

您不是在评估 IF 的条件.如果您在 fileToCheck 中找到 stringToCheck,我猜您不想复制.你需要做一些类似的事情(代码未经测试,但你明白了):

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:OtherFolderfileToCheck.bat" "C:MyFolder" /s /y

dbenham 编辑
上面的测试是错误的,它总是评估为 FALSE.
正确的测试是 IF ERRORLEVEL 1 XCOPY ...

更新:我无法测试代码,但我不确定如果 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:OtherFolderfileToCheck.bat" "C:MyFolder" /s /y
del tempfindoutput.txt

这篇关于如果 FINDSTR 找不到字符串,如何有条件地采取行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆