I’m trying to make a sub in batch that find a string in a string and replace it with a third one.
Searching around, I found here that I can erase a string with the SET command.
So, here’s what I tried:
:modifyString what with [in]
SET _what=%~1
ECHO "%_what%"
SET "_with=%~2
ECHO "%toWith%"
SET _In=%~3
ECHO "%_In%"
SET _In=%_In:%toWhat%=%toWith%%
ECHO %_In%
SET "%~3=%_In%"
EXIT /B
The ECHOs are there only for “debugging” purposes.
What I know is that the error is in…
SET _In=%_In:%toWhat%=%toWith%%
…because of the % character that close the %_in% variable.
I tried also things such as…
SET _In=%_In:!toWhat!=!toWith!%
SET _In=!_In:%toWhat%=%toWith%!
…and others without sense.
This is the main problem, the other is to return %_In% in [in].
Any tips?
Here is an example using the
!DelayedExpansion method.Output
Fixed Yours
How to do the substring replacement without delayed expansion. Use the
callcommand to create two levels of variable expansion. Use single%around variables to expand first and double%%around variables to expand second.