Skip to content

Instantly share code, notes, and snippets.

@richard087
Last active April 15, 2024 09:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save richard087/6217537 to your computer and use it in GitHub Desktop.
Save richard087/6217537 to your computer and use it in GitHub Desktop.
Batch file to do a token replacement on a text file, in Windows.
@echo off
rem sourced from http://stackoverflow.com/questions/5273937/how-to-replace-substrings-in-windows-batch-file
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%
@NabilDel
Copy link

Very useful script - thanks for sharing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment