Reputation: 1025
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 ECHO
s 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?
Upvotes: 2
Views: 716
Reputation: 1025
Thank you all guys!
I paste you what I finally made:
:modifyString what with in tRtn
set "_in=%~3"
set "_in=!_in:%~1=%~2!"
IF NOT "%~4" == "" SET %~4=%_in%
EXIT /B
E.G. If I call this sub in this way:
SET "str=All your base are belong to us"
SET "toFind=your base are belong"
SET "space=of your bases belong"
ECHO %str%
CALL :modifyString "%toFind%" "%space%" "%str%" string
%string% would became the new corrected string, so if you do...
ECHO "%string%"
it would print:
"All of your bases belong to us"
P.S. I'm sorry if I'm late, but because of my low reputation i had to wait!
Upvotes: 2
Reputation: 11367
Here is an example using the !
DelayedExpansion method.
@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "xxString=All your base are belong to us"
set "xxSubString=your base are belong"
set "xxNewSubString=of your bases belong"
echo Before
set xx
echo.
set "xxString=!xxString:%xxSubString%=%xxNewSubString%!"
echo After
set xx
endlocal
pause >nul
Output
Before
xxNewSubString=of your bases belong
xxString=All your base are belong to us
xxSubString=your base are belong
After
xxNewSubString=of your bases belong
xxString=All of your bases belong to us
xxSubString=your base are belong
Fixed Yours
@echo off
:: Make sure that you have delayed expansion enabled.
setlocal EnableDelayedExpansion
:modifyString what with [in]
SET "_what=%~1"
SET "_with=%~2"
SET "_In=%~3"
ECHO "%_what%"
ECHO "%_with%"
ECHO "%_In%"
:: The variable names were not the same as the ones
:: defined above.
SET _In=!_In:%_what%=%_with%!
ECHO %_In%
:: This will not change the value of the 3rd parameter
:: but instead will create a new parameter with the
:: value of %3 as the variable name.
SET "%~3=%_In%"
endlocal
EXIT /B
How to do the substring replacement without delayed expansion. Use the call
command to create two levels of variable expansion. Use single %
around variables to expand first and double %%
around variables to expand second.
@echo off
setlocal EnableExtensions
set "xxString=All your base are belong to us"
set "xxSubString=your base are belong"
set "xxNewSubString=of your bases belong"
echo Before
set xx
echo.
call set "xxString=%%xxString:%xxSubString%=%xxNewSubString%%%"
echo After
set xx
endlocal
pause >nul
Upvotes: 4