How to replace substrings in a windows batch file

Can someone tell me using a batch file on Windows ... how to read from a file and replace the = bath line from the file containing = bath Abath Bbath XYZbathABC with the hello line so that the result looks like hello Ahello Bhello XYZhelloABC

+58
batch-file
Mar 11 2018-11-11T00:
source share
7 answers

Extension from Andriy M, and yes, you can do it from a file, even one with several lines

 @echo off setlocal enabledelayedexpansion set INTEXTFILE=test.txt set OUTTEXTFILE=test_out.txt set SEARCHTEXT=bath set REPLACETEXT=hello set OUTPUTLINE= for /f "tokens=1,* delims=ΒΆ" %%A in ( '"type %INTEXTFILE%"') do ( SET string=%%A SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%! echo !modified! >> %OUTTEXTFILE% ) del %INTEXTFILE% rename %OUTTEXTFILE% %INTEXTFILE% 

EDIT

Thanks to David Nelson, I updated the script, so it no longer has hardcoded values.

+72
Mar 11 '11 at 15:31
source share
β€” -
 SET string=bath Abath Bbath XYZbathABC SET modified=%string:bath=hello% ECHO %string% ECHO %modified% 

EDIT

At first, it wasn’t visible that you needed the replacement to be preceded by reading a line from a file.

Well, with a batch file you have few options for working with files. In this particular case, you will need to read the line, perform a replacement, then output the modified line, and then ... What then? If you need to replace all the "baths" in the entire file, you will have to use a loop:

 @ECHO OFF SETLOCAL DISABLEDELAYEDEXPANSION FOR /F %%L IN (file.txt) DO ( SET "line=%%L" SETLOCAL ENABLEDELAYEDEXPANSION ECHO !line:bath=hello! ENDLOCAL ) ENDLOCAL 

You can add a redirect to the file:

  ECHO !line:bath=hello!>>file2.txt 

Or you can apply the redirection to the batch file. It must be a different file.

EDIT 2

Added the correct switch of slow expansion for the correct processing of some characters that have special meaning with the syntax of a batch script, for example ! , ^ et al. (Thanks jeb !)

+35
Mar 11 2018-11-11T00:
source share

To avoid an empty space (to give readability in the conf file), I combine aflat and jeb answer ( here ) something like this:

 @echo off setlocal enabledelayedexpansion set INTEXTFILE=test.txt set OUTTEXTFILE=test_out.txt set SEARCHTEXT=bath set REPLACETEXT=hello set OUTPUTLINE= for /f "tokens=1,* delims=ΒΆ" %%A in ( '"findstr /n ^^ %INTEXTFILE%"') do ( SET string=%%A for /f "delims=: tokens=1,*" %%a in ("!string!") do set "string=%%b" if "!string!" == "" ( echo.>>%OUTTEXTFILE% ) else ( SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%! echo !modified! >> %OUTTEXTFILE% ) ) del %INTEXTFILE% rename %OUTTEXTFILE% %INTEXTFILE% 
+10
Nov 26 '13 at 20:25
source share

To avoid problems with a parsing parser (for example, an exclamation mark), see Problem finding and replacing a batch file .

The next change in aflat script will include special characters such as exclamation points.

 @echo off setlocal DisableDelayedExpansion set INTEXTFILE=test.txt set OUTTEXTFILE=test_out.txt set SEARCHTEXT=bath set REPLACETEXT=hello set OUTPUTLINE= for /f "tokens=1,* delims=ΒΆ" %%A in ( '"type %INTEXTFILE%"') do ( SET string=%%A setlocal EnableDelayedExpansion SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%! >> %OUTTEXTFILE% echo(!modified! endlocal ) del %INTEXTFILE% rename %OUTTEXTFILE% %INTEXTFILE% 
+3
Jun 04 '13 at 13:27
source share

To avoid an empty space, simply replace this:

 echo !modified! >> %OUTTEXTFILE% 

with this:

 echo.!modified! >> %OUTTEXTFILE% 
-one
Sep 27 '16 at 20:16
source share

This is much simpler:

 C:\Users\usera>set var="bath Abath Bbath XYZbathABC" C:\Users\usera>echo %var:bath=hello% "hello Ahello Bhello XYZhelloABC" 
-2
Jun 16 '16 at 17:48
source share

If you have Ruby for Windows ,

 C:\>more file bath Abath Bbath XYZbathABC C:\>ruby -pne "$_.gsub!(/bath/,\"hello\")" file hello Ahello Bhello XYZhelloABC 
-6
Mar 11 2018-11-11T00:
source share



All Articles