遍历文本文件以查找字符串批处理

时间:2018-11-15 15:43:00

标签: batch-file findstr

我想在所有.config文件中搜索字符串“ Hello”,如果找到了,我想调用函数findString。如果没有,我希望它继续搜索.config文件。

这就是我所拥有的,我看不出是什么问题。

@echo off
setlocal EnableExtensions


:TOP
for /R %%f in (*.config) do (
    findstr /i "Hello" "%%f" >NUL
    if errorlevel 1 (
        call :findString
    ) else (
        goto TOP
    )
    pause 
    exit /b
)

:findString
    set "textfile=%1"

1 个答案:

答案 0 :(得分:1)

使用 Dave Benham 编写的JREPL.BAT可以轻松地在所有未隐藏的* .config文件中递归搜索一个字符串并将其替换为另一个字符串。批处理文件/ JScript混合文件,以使用JScript在文件上运行正则表达式替换。

@echo off
setlocal EnableExtensions DisableDelayedExpansion
cls
if not exist "%~dp0jrepl.bat" (
    echo Batch file "%~nx0" requires batch file JREPL.BAT in
    echo directory: "%~dp0"
    echo/
    echo Please download it from:
    echo/
    echo https://www.dostips.com/forum/viewtopic.php?f=3^&t=6044
    echo/
    echo Extract the batch file in downloaded ZIP archive file into
    echo directory: "%~dp0"
    goto EndBatch
)

set "Search=Hello"
set "Replace=Goodbye"

echo Processing *.config files in entire directory tree starting in:
echo "%CD%"
echo/

set "FilesCount=0"
set "FoundCount=0"

for /F "eol=| delims=" %%I in ('dir *.config /A-D-H /B /S 2^>nul') do (
    set /A FilesCount+=1
    %SystemRoot%\System32\findstr.exe /M /I /L /C:"%Search%" "%%I" >nul
    if errorlevel 1 (
        echo File "%%I" does not contain "%Search%".
    ) else (
        set /A FoundCount+=1
        echo File "%%I" contains "%Search%" replaced by "%Replace%".

        rem Replace case-insensitive literally the string "%Search%" by string "%Replace%".
        call "%~dp0jrepl.bat" "%Search%" "%Replace%" /I /L /F "%%I" /O -

        rem Insert here more command lines to execute on *.config file
        rem containing literally the string to find in the file.
    )
)
echo/
if %FilesCount% == 1 (set "PluralS=") else set "PluralS=s"
echo Updated %FoundCount% of %FilesCount% *.config file%PluralS%.

:EndBatch
endlocal
echo/
pause

使用的主要原因

for /F "eol=| delims=" %%I in ('dir *.config /A-D-H /B /S 2^>nul') do (

代替

for /R %%I in (*.config) do (

事实是,如果一个目录包含多个* .config文件,则后者在带有FAT32 or ExFAT文件系统的驱动器上无法正常工作。在具有NTFS文件系统的驱动器上,也可以使用仅使用 FOR 的较短和较快的命令行。

下一个解决方案使用 FINDSTR 递归搜索* .config文件,该文件包含字面值且不区分大小写的字符串Hello,用Goodbye替换。

@echo off
setlocal EnableExtensions DisableDelayedExpansion
cls
if not exist "%~dp0jrepl.bat" (
    echo Batch file "%~nx0" requires batch file JREPL.BAT in
    echo directory: "%~dp0"
    echo/
    echo Please download it from:
    echo/
    echo https://www.dostips.com/forum/viewtopic.php?f=3^&t=6044
    echo/
    echo Extract the batch file in downloaded ZIP archive file into
    echo directory: "%~dp0"
    goto EndBatch
)

set "Search=Hello"
set "Replace=Goodbye"

echo Processing *.config files in entire directory tree starting in:
echo "%CD%"
echo/

set "FoundCount=0"

for /F "eol=| delims=" %%I in ('%SystemRoot%\System32\findstr.exe /M /I /L /S /C:"%Search%" "*.config"') do (
    set /A FoundCount+=1
    echo File "%%I" contains "%Search%" replaced by "%Replace%".

    rem Replace case-insensitive literally the string "%Search%" by string "%Replace%".
    call "%~dp0jrepl.bat" "%Search%" "%Replace%" /I /L /F "%%I" /O -

    rem Insert here more command lines to execute on *.config file
    rem containing literally the string to find in the file.
)
echo/
if %FoundCount% == 1 (set "PluralS=") else set "PluralS=s"
echo Updated %FoundCount% *.config file%PluralS%.

:EndBatch
endlocal
echo/
pause

当然,此解决方案不会报告不包含搜索字符串的非隐藏* .config文件,因为这些文件已被 FINDSTR 过滤掉。

这两个批处理文件甚至在Windows XP和Windows Server 2003上也可以工作。

仅在了解以下情况的情况下,才可以建议不使用 JREPL.BAT 且仅使用Windows命令处理器的非常有限的功能来处理字符串的其他解决方案

    * .config文件的
  • character encoding
  • 那些* .config文件真正包含和的行
  • 要搜索的字符串是什么
  • 什么是替换字符串。

cmd.exe不适用于编辑文本文件。它是为运行命令和应用程序而设计的。许多其他脚本语言或它们的解释器都非常易于使用内置函数来按字面替换一个或多个文件中的字符串,或使用正则表达式(例如JScript,PowerShell,Python,Perl等)

例如,仅使用命令 FOR SET来查看Modify a string in a .properties file with batchRegex in Batch Windows的难度以及为一个简单的字符串替换可以写入多少变体cmd.exe提供了用于处理字符串的功能。发问者至少已经发布了要处理的文件包含的内容以及他们对批处理文件的期望,尽管两个发问者也没有发布完整的需求列表。

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面。

  • call /? ...还解释了%~dp0(批处理文件的驱动器和路径始终以反斜杠扩展)和%~nx0(仅批处理文件的文件名和扩展名),原因如下:参数0始终是批处理文件。
  • cls /?
  • echo /?
  • endlocal /?
  • findstr /?
  • for /?
  • if /?
  • jrepl.bat /?
  • pause /?
  • rem /?
  • set /?
  • setlocal /?

有关Windows命令的更多更好的帮助,请访问:

相关问题