我正在尝试为Windows XP创建一个批处理(.bat)文件,以执行以下操作:
If (file.txt contains the string 'searchString') then (ECHO found it!)
ELSE(ECHO not found)
到目前为止,我找到了一种方法,使用 FIND 命令在文件中搜索字符串,该命令返回文件中找到字符串的行,但无法进行条件检查在它上面。
例如,这不起作用。
IF FIND "searchString" file.txt ECHO found it!
这也不是:
IF FIND "searchString" file.txt=='' ECHO not found
关于如何做到这一点的任何想法?
答案 0 :(得分:9)
来自其他帖子:
find /c "string" file
if %errorlevel% equ 1 goto notfound
echo found
goto done
:notfound
echo notfound
goto done
:done
或类似:如果没有找到写入文件。
find /c "%%P" file.txt || ( echo %%P >> newfile.txt )
或类似:如果找到写入文件。
find /c "%%P" file.txt && ( echo %%P >> newfile.txt )
或类似的东西:
find /c "%%P" file.txt && ( echo found ) || ( echo not found )
答案 1 :(得分:3)
我已经使用 DOS 命令行来执行此操作。 实际上是两条线。第一个制作"当前目录"文件所在的文件夹 - 或文件所在的一组文件夹的根文件夹。第二行进行搜索。
CD C:\TheFolder
C:\TheFolder>FINDSTR /L /S /I /N /C:"TheString" *.PRG
您可以在this link找到有关参数的详细信息。
希望它有所帮助!