0

好吧,我相信我正在用这个突破批处理文件的限制。

我需要一个批处理文件,它会在文件中查找“X”或“y”。如果找到任何一个,运行一个程序。如果两者均未找到,请继续执行其余代码。它将在其中查找的文件具有扩展名 .inf。它是使用记事本打开的。我什至不知道从哪里开始。任何帮助将不胜感激。:)

4

3 回答 3

2

您可以使用 FINDSTR 同时搜索多个条目。像这样使用它:

FINDSTR "term1 term2 term3 ..."

如果至少找到一个术语,则结果将是成功的。如有必要,使用/I开关使搜索不区分大小写:

FINDSTR /I "term1 term2 term3 ..."

FINDSTR默认搜索stdin。将输入重定向到您的.inf文件以使其搜索文件:

FINDSTR /I "term1 term2 term3 ..." <file.inf

或者,您可以将文件名作为另一个参数:

FINDSTR /I "term1 term2 term3 ..." file.inf

这两种情况下的输出会略有不同,但我知道您实际上并不需要输出,而是搜索的结果,即它是成功还是失败。

要检查结果,您可以使用显式ERRORLEVEL测试,如下所示:

FINDSTR /I "term1 term2 term3 ..." file.inf
IF NOT ERRORLEVEL 1 yourprogram.exe

另一种语法是使用ERRORLEVEL 系统变量,这可能比前者更直接:

IF %ERRORLEVEL% == 0 yourprogram.exe

另一种方法是使用&&运算符。这种方法与test并不完全相同ERRORLEVEL但test和workFINDSTR都是一样的,在这种情况下,这种方法的优点是更简洁:ERRORLEVEL&&&&

FINDSTR /I "term1 term2 term3 ..." file.inf && yourprogram.exe

这差不多了。最后一点是,由于您可能对 的输出实际上并不感兴趣FINDSTR,您不妨通过将其重定向到 来抑制它NUL,如下所示:

FINDSTR /I "term1 term2 term3 ..." file.inf >NUL && yourprogram.exe
于 2013-01-30T20:39:13.200 回答
0

FIND 和 FC 的组合可以做到这一点。

@echo off
REM try to find X
FIND /c "X" file.inf >test_isxfound.txt
REM what does it look like when I don't find a match
FIND /c "th1s$tringb3tt3rn0tbeinthisfile" file.inf >test_xnotfound.txt
REM then compare those results with the results where we know it wasn't found
FC test_xnotfound.txt test_isxfound.txt
REM then check to see if FC saw a difference
IF ERRORLEVEL 1 goto xisfound

ECHO *** X is not found
goto end
:xisfound
ECHO *** X is found
goto end
:end
del test_xnotfound.txt
del test_isxfound.txt
于 2013-01-30T20:27:02.020 回答
0

尝试从这个页面开始:

http://www.robvanderwoude.com/findstr.php

然后进一步参考批处理文件的基础知识:

http://www.robvanderwoude.com/batchfiles.php

于 2013-01-30T18:03:10.413 回答