Batch file 如何替换Windows批处理文件中的字符串

Batch file 如何替换Windows批处理文件中的字符串,batch-file,Batch File,我想替换文件中的以下字符串: android:versionName="anyStringHere" > *anyStringHere表示任何可能的字符串 与: 如何以干净、可重复使用的方式执行此操作,并在文件中保留新行、制表符和缩进?甚至不接近最快的选项,也不是100%防弹,但这是纯批量操作,在替换时将处理间距和缩进 @echo off setlocal enableextensions disabledelayedexpansion rem File to proc

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

android:versionName="anyStringHere" >
*anyStringHere表示任何可能的字符串

与:


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

甚至不接近最快的选项,也不是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"
@echo关闭
setlocal enableextensions disabledelayedexpansion
要处理的rem文件
设置“file=data.txt”
rem如何找到线条
设置“匹配=公共静态字符串常量=\“abc\”
要替换和替换的rem字符串
设置“findStr=abc”
设置“replaceStr=def”
要使用行的rem临时文件
设置“tempFile=%temp%\repl.tmp”
rem所有输出都进入临时文件
(
rem进程输入文件提取不匹配行
对于/f令牌^=^1^*^delims^=^:^eol^=%%a in('findstr/n/v/c:'%match%'^<%file%')do(
设置/a“n=1000000+%%a”
延迟扩展
%tempFile%
rem对输出文件进行排序以获得最终文件
(对于/f标记^=^1^*^delims^=^:^eol^=%%%a in('排序“%tempFile%”)do(
如果“%%b”==”(
回响
)否则(
回声%%b
)
))>%file%.repl

这是我能想到的最简单的方法。它获取一个字符串并在文件中搜索,然后替换包含该字符串的整行。它不仅可以替换一条生产线的一部分,这需要付出更多的努力

@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循环上的echo更改为输出到不同的文件,可能是
%file%.new
或其他文件,然后删除
del
命令。

这是一个保留所有格式的健壮解决方案。它使用名为
repl.bat的助手批处理文件
-下载自:

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

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

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


您是在问如何使用批处理命令执行替换吗?是的,我看到了的答案,但答案没有保留新行。请看一下如何使用替换字符串。我正在专门查找Windows批处理文件solution@newuser,您可以使用第三方工具吗?
type "file.txt" | repl "(public static String CONST = \q).*(\q.*)" "$1def$2" x >"newfile.txt"
sed "s/android:versionName=\".*\" >/android:versionName=\"%NEW_VERSION%\" >/g" %ORIG_FILE_NAME% > %TEMP_FILE_NAME%

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