How to replace Strings in Windows Batch file

前端 未结 4 531
误落风尘
误落风尘 2020-12-22 08:20

I would like to replace the following String in a file:

android:versionName=\"anyStringHere\" >

*anyStringHere represents any possible s

相关标签:
4条回答
  • 2020-12-22 09:00

    I found that using sed was the cleanest solution

    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
    
    0 讨论(0)
  • 2020-12-22 09:01

    Not even close to the fastest option, and not 100% bulletproof, but this is pure batch and will handle spacing and indentation while do the replacement.

    @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"
    
    0 讨论(0)
  • 2020-12-22 09:06

    This is a robust solution that retains all formatting. It uses a helper batch file called repl.bat - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

    Place repl.bat in the same folder as the batch file or in a folder that is on the path.

    type "file.txt" | repl "(public static String CONST = \q).*(\q.*)" "$1def$2" x >"newfile.txt"
    
    0 讨论(0)
  • 2020-12-22 09:16

    This is the simplest way to do this that I could come up with. It takes a String and searches for it in a file, then replaces the entire line that contains the string. It won't only replace parts of a line, which can be done with a bit more effort.

    @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
    

    You can change the echo on the last for loop to output to a different file, maybe %file%.new or something, and then remove the del command.

    0 讨论(0)
提交回复
热议问题