批处理文件:如何搜索和替换具有" ="的字符串内

时间:2016-06-09 11:14:34

标签: batch-file replace cmd

我必须从类似Pippo.K = 5的txt中搜索字符串,并将其替换为Pippo.K = 1。我需要搜索整个字符串。我做的是:

set "search=Pippo.K=5"
set "replace=Pippo.K=1"
set "textFile=%SettingFile%.txt"
for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%search%=%replace%!"
    >>"%textFile%" echo(!line!
    endlocal
)

但我回来的是 5=Pippo.K=1=5

如何解决此错误?

2 个答案:

答案 0 :(得分:2)

以下脚本构成纯解决方案。假设它存储为repl-str.bat,您需要为您的应用程序调用它:

repl-str.bat "%SettingFile%.txt" "Pippo.K=5" "Pippo.K=1" "%SettingFile%.txt"

这指定输入文件%SettingFile%.txt,文字和区分大小写的搜索字符串Pippo.K=5,替换字符串Pippo.K=1和输出文件%SettingFile%.txt,它们与输入文件(相关技术取自这个答案:Batch script to find and replace a string in text file without creating an extra output file for storing the modified file)。如果没有给出输出文件,结果将输出到控制台(对测试很有用)。

以下是脚本repl-str.bat的代码:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

set "FILE_I=%~1"
set "SEARCH=%~2"
set "REPLAC=%~3"
set "FILE_O=%~4"
set "FLAG=%~5"
if not defined FILE_I exit /B 1
if not defined SEARCH exit /B 1
if not defined FILE_O set "FILE_O=con"
if defined FLAG set "FLAG=#"

for /F "delims=" %%L in ('
    findstr /N /R "^" "%FILE_I%" ^& break ^> "%FILE_O%"
') do (
    set "STRING=%%L"
    setlocal EnableDelayedExpansion
    set "STRING=!STRING:*:=!"
    call :REPL RETURN STRING SEARCH REPLAC %FLAG%
    >> "%FILE_O%" echo(!RETURN!
    endlocal
)

endlocal
exit /B


:REPL  rtn_string  ref_string  ref_search  ref_replac  flag
setlocal EnableDelayedExpansion
set "STR=!%~2!"
set "SCH=!%~3!"
set "RPL=!%~4!"
if not defined SCH endlocal & set "%~1=" & exit /B 1
set "SCH_CHR=!SCH:~,1!"
if not "%~5"=="" set "SCH_CHR="
if "!SCH_CHR!"=="=" set "SCH_CHR=" & rem = terminates search string
if "!SCH_CHR!"==""^" set "SCH_CHR=" & rem " could derange syntax
if "!SCH_CHR!"=="%%" set "SCH_CHR=" & rem % ends variable expansion
if "!SCH_CHR!"=="^!" set "SCH_CHR=" & rem ! ends variable expansion
call :LEN SCH_LEN SCH
call :LEN RPL_LEN RPL
set /A RED_LEN=SCH_LEN-1
set "RES="
:LOOP
call :LEN STR_LEN STR
if not defined STR goto :END
if defined SCH_CHR (
    set "WRK=!STR:*%SCH_CHR%=!"
    if "!WRK!"=="!STR!" (
        set "RES=!RES!!STR!"
        set "STR="
    ) else (
        call :LEN WRK_LEN WRK
        set /A DFF_LEN=STR_LEN-WRK_LEN-1,INC_LEN=DFF_LEN+1,MOR_LEN=DFF_LEN+SCH_LEN
        for /F "tokens=1,2,3 delims=," %%M in ("!DFF_LEN!,!INC_LEN!,!MOR_LEN!") do (
            rem set "RES=!RES!!STR:~,%%M!"
            if defined WRK set "WRK=!WRK:~,%RED_LEN%!"
            if "!STR:~%%M,1!!WRK!"=="!SCH!" (
                set "RES=!RES!!STR:~,%%M!!RPL!"
                set "STR=!STR:~%%O!"
            ) else (
                set "RES=!RES!!STR:~,%%N!"
                set "STR=!STR:~%%N!"
            )
        )
    )
) else (
    if "!STR:~,%SCH_LEN%!"=="!SCH!" (
        set "RES=!RES!!RPL!"
        set "STR=!STR:~%SCH_LEN%!"
    ) else (
        set "RES=!RES!!STR:~,1!"
        set "STR=!STR:~1!"
    )
)
goto :LOOP
:END
if defined RES (
    for /F delims^=^ eol^= %%S in ("!RES!") do (
        endlocal
        set "%~1=%%S"
    )
) else endlocal & set "%~1="
exit /B


:LEN  rtn_length  ref_string
setlocal EnableDelayedExpansion
set "STR=!%~2!"
if not defined STR (set /A LEN=0) else (set /A LEN=1)
for %%L in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
    if defined STR (
        set "INT=!STR:~%%L!"
        if not "!INT!"=="" set /A LEN+=%%L & set "STR=!INT!"
    )
)
endlocal & set "%~1=%LEN%"
exit /B

基本上,此方法采用搜索字符串的第一个字符并在输入文本中查找。在每次匹配时,检查是否发生整个搜索字符串。如果是,则通过删除搜索字符串所包含的字符数来替换替换字符串,从而避免在搜索字符串包含=或搜索或替换字符串时失败的子字符串替换语法包含%! 但是,如果搜索字符串的第一个字符是="%!,则方法不同,脚本会检查每个字符的位置是否发生然后搜索字符串,具有降低整体性能的缺点。如果给出第五个命令行参数(任意值),则强制执行此(慢)模式。

答案 1 :(得分:1)

批量变量子串替换确实有局限性。处理文字等号是其中之一。

targetTicks = Environment.TickCount + (10 * 1000); //Initial timer configuration.
await WaitUntilTimeoutAsync();
InvokeMyAction();

会奏效。 PowerShell单行代码是powershell "(gc \"%textFile%\") -replace '%search%','%replace%'" 循环的简单替代方案,没有这种限制。

如果你喜欢for /f循环,如果你的文本文件是ini风格的文件,试试这个:

for /F

请注意,如果文件中的任何项目具有空白值(例如@echo off & setlocal set "searchItem=Pippo.K" set "searchVal=5" set "newVal=1" set "textFile=test.txt" >"outfile.txt" ( for /f "eol=; usebackq tokens=1* delims==" %%I in ("%textFile%") do ( if /I "%%~I"=="%searchItem%" ( if "%%~J"=="%searchVal%" ( echo %%I=%newVal% ) else echo %%I=%%J ) else ( if not "%%~J"=="" (echo %%I=%%J) else echo %%I ) ) ) move /y "outfile.txt" "%textFile%" ),则除非您添加一些额外的逻辑,否则将删除等号。

您也可以考虑使用ini.bat from this answer