Last active
April 15, 2024 09:53
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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
Very useful script - thanks for sharing.