I am actually trying to replace a part of the string in a text file.
If my old file contains this text
12345qwert91202030000000
I want to change ‘120203’ to ‘120204’. This is my code:
setlocal enabledelayedexpansion
if exist c:\myfile.txt (set b=c:\myfile.txt) ELSE GOTO LOGO
for /f "tokens= 1" %%c in (%b%) do (set line=%%c)
Set NEWdate=120204
ECHO %line:~0,11%%NEWDate%%line:~17,7% > %b%
This works just fine. Now, if my old file contains the following text
12345q&ert91202030000000
Note the symbol ‘&’, my code doesn’t work as it takes it as a code.
Any suggestions?
You should use delayed expansion in this case, as percent expansion will fail with special characters, but delayed expansion doesn’t recognize them.
Simply change the percents with exclamation marks, you enabled the delayed expansion before.