Skip to content

Instantly share code, notes, and snippets.

@opicron
Forked from richard087/tokenReplace.bat
Created April 15, 2024 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save opicron/676030ca7ab58f773f9ee7b75465ce3c to your computer and use it in GitHub Desktop.
Save opicron/676030ca7ab58f773f9ee7b75465ce3c 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%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment