如何在Windows批处理文件中替换字符串

时间:2014-01-07 19:09:28

标签: batch-file

我想在文件中替换以下字符串:

android:versionName="anyStringHere" >

* anyStringHere表示任何可能的字符串

使用:

android:versionName="1.04.008" >

如何以干净,可重复使用的方式执行此操作,并在文件中保留新行,制表符和缩进?

4 个答案:

答案 0 :(得分:0)

甚至没有接近最快的选项,也不是100%防弹,但这是纯批次,并且会在替换时处理间距和缩进。

@echo off
    setlocal enableextensions disabledelayedexpansion

    rem File to process
    set "file=data.txt"

    rem How to find lines
    set "match=public static String CONST = \"abc\";"

    rem String to replace and replacement
    set "findStr=abc"
    set "replaceStr=def"

    rem temporary file to work with lines
    set "tempFile=%temp%\repl.tmp"

    rem All the output goes into the temporary file
    (

        rem Process input file extracting non matching lines
        for /f tokens^=^1^*^ delims^=^:^ eol^= %%a in ('findstr /n /v /c:"%match%" ^< "%file%"') do (
            set /a "n=1000000+%%a" 
            setlocal enabledelayedexpansion
            < nul set /p "n=!n!"
            endlocal
            echo :%%b
        )

        rem Process input file extrancting matching lines and changing strings
        for /f tokens^=^1^*^ delims^=^:^ eol^= %%a in ('findstr /n /c:"%match%" ^< "%file%"') do (
            set /a "n=1000000+%%a" 
            set "data=%%b"
            setlocal enabledelayedexpansion
            set "data=!data:%findStr%=%replaceStr%!"
            echo !n!:!data!
            endlocal
        ) 
    )> "%tempFile%"

    rem Sort the output file to get the final file
    (for /f tokens^=^1^*^ delims^=^:^ eol^= %%a in ('sort "%tempFile%"') do (
        if "%%b"=="" ( 
            echo.
        ) else (
            echo %%b
        )
    )) > "%file%.repl"

答案 1 :(得分:0)

这是我能想到的最简单的方法。它需要一个String并在文件中搜索它,然后替换包含该字符串的整行。它不仅会替换一行的部分,而且可以用更多的努力来完成

@echo off

:: file containing string to replace
set file=test.txt
:: string to replace in file
set searchString=line 4
:: string to write to file
set repString=line 4 edited

setLocal enableDelayedExpansion
set count=0

if not exist %file% echo cannot find file - %file% & goto :EOF

:: Search for string - and get it's line number
for /F "delims=:" %%a in ('findstr /N /I /C:"%searchString%" "%file%"') do set searchLine=%%a

if not defined searchLine echo cannot find string - %searchString% - in file - %file% & goto :EOF

:: Read file into variables - by line number
for /F "delims=~!" %%b in ('type %file%') do (
    set /a count=!count!+1
    set line!count!=%%b
)

:: Edit the one line
set line%searchLine%=%repString%

:: Empty file and write new contents
del %file%
for /L %%c in (1,1,!count!) do echo !line%%c!>>%file%

pause

您可以更改最后一个for循环的回显以输出到另一个文件,可能是%file%.new或其他内容,然后删除del命令。

答案 2 :(得分:0)

这是一个强大的解决方案,可以保留所有格式。它使用名为repl.bat的帮助程序批处理文件 - 从https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

下载

repl.bat放在与批处理文件相同的文件夹中或放在路径上的文件夹中。

type "file.txt" | repl "(public static String CONST = \q).*(\q.*)" "$1def$2" x >"newfile.txt"

答案 3 :(得分:0)

我发现使用sed是最干净的解决方案

http://gnuwin32.sourceforge.net/packages/sed.htm

sed "s/android:versionName=\".*\" >/android:versionName=\"%NEW_VERSION%\" >/g" %ORIG_FILE_NAME% > %TEMP_FILE_NAME%

@move /Y %TEMP_FILE_NAME% %ORIG_FILE_NAME% >nul