如何使用 Windows 命令行查找文件是否包含给定字符串

发布于 2024-10-31 15:30:11 字数 468 浏览 3 评论 0原文

我正在尝试为 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

关于如何做到这一点有任何想法吗?

I am trying to create a batch (.bat) file for windows XP to do the following:

If (file.txt contains the string 'searchString') then (ECHO found it!) 
ELSE(ECHO not found)

So far, I have found a way to search for strings inside a file using the FIND command which returns the line in the file where it finds the string, but am unable to do a conditional check on it.

For example, this doesn't work.

IF FIND "searchString" file.txt ECHO found it!

Nor does this:

IF FIND "searchString" file.txt=='' ECHO not found

Any Ideas on how this can be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

窗影残 2024-11-07 15:30:11

来自其他帖子:

    find /c "string" file >NUL
    if %errorlevel% equ 1 goto notfound
        echo found
    goto done
    :notfound
        echo notfound
    goto done
    :done

当您想要不区分大小写的检查时,请使用 /i 开关:

    find /i /c "string" file >NUL

或者类似的内容:如果找不到,则写入文件。

    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 )

From other post:

    find /c "string" file >NUL
    if %errorlevel% equ 1 goto notfound
        echo found
    goto done
    :notfound
        echo notfound
    goto done
    :done

Use the /i switch when you want case insensitive checking:

    find /i /c "string" file >NUL

Or something like: if not found write to file.

    find /c "%%P" file.txt  || ( echo %%P >> newfile.txt )

Or something like: if found write to file.

    find /c "%%P" file.txt  && ( echo %%P >> newfile.txt )

Or something like:

    find /c "%%P" file.txt  && ( echo found ) || ( echo not found )
送君千里 2024-11-07 15:30:11

我使用 DOS 命令行来执行此操作。
实际上是两条线。第一个将“当前目录”设为文件所在的文件夹 - 或文件所在的一组文件夹的根文件夹。第二行进行搜索。

CD C:\TheFolder
C:\TheFolder>FINDSTR /L /S /I /N /C:"TheString" *.PRG

您可以在此链接找到有关参数的详细信息。

希望有帮助!

I've used a DOS command line to do this.
Two lines, actually. The first one to make the "current directory" the folder where the file is - or the root folder of a group of folders where the file can be. The second line does the search.

CD C:\TheFolder
C:\TheFolder>FINDSTR /L /S /I /N /C:"TheString" *.PRG

You can find details about the parameters at this link.

Hope it helps!

叹沉浮 2024-11-07 15:30:11

另一个解决方案:

type "file.txt" | findstr "searchString" >nul

if %ERRORLEVEL% equ 0 (
    echo found it!
) else (
    echo not found
)

Another solution:

type "file.txt" | findstr "searchString" >nul

if %ERRORLEVEL% equ 0 (
    echo found it!
) else (
    echo not found
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文