For loop 未通过“获取所需的输出”;查找“;Windows批处理脚本中for循环中的命令

For loop 未通过“获取所需的输出”;查找“;Windows批处理脚本中for循环中的命令,for-loop,batch-file,find,For Loop,Batch File,Find,我的要求很简单,我只想扫描当前目录中的所有文件以查找特定字符串,如果找到该字符串,我只想显示“string is found”,否则显示“string not found” 但问题是,当我没有将其放入for循环时,但当我出于某种原因将其放入for循环时,我得到的每个文件都会找到一个不正确的消息字符串。使用条件运算符&&和| &&为iferrorleveleq 0 |为iferrorlevelneq 0 @echo off for %%f in ("%userprofile%\Deskt

我的要求很简单,我只想扫描当前目录中的所有文件以查找特定字符串,如果找到该字符串,我只想显示“string is found”,否则显示“string not found”


但问题是,当我没有将其放入for循环时,但当我出于某种原因将其放入for循环时,我得到的每个文件都会找到一个不正确的消息字符串。

使用条件运算符
&&
|

&&
为if
errorlevel
eq 0

|
为if
errorlevel
neq 0

@echo off
for %%f in ("%userprofile%\Desktop\BatchScript\*") do (
    echo File is %%f
    find /c "defaultModel" %%f >nul 2>&1 && echo String found || echo String NOT found
)

我建议打开一个,运行
if/?
,然后阅读输出帮助,该帮助在第一页已经解释了评估前一个运行命令或可执行文件的退出代码的建议语法。因此,如果使用
if errorlevel 1
而不是
if%errorlevel%eq 1
,则代码可以工作,您不必考虑延迟扩展。另请参阅和第4章。非常感谢各位。如果errorlevel有效…而且不必使用delayedvariable扩展逻辑,我们为此奋斗了一个多星期。实际使用了Mofo给出的评论。。那么,我如何才能将其标记为已接受的答案呢?
@echo off
for %%f in ("%userprofile%\Desktop\BatchScript\*") do (
    echo File is %%f
    find /c "defaultModel" %%f >nul 2>&1 && echo String found || echo String NOT found
)