I'm having trouble figuring out a way to remove a substring from a string when both may have reserved characters in it. Nothing I've attempted has had any luck.
I'm new to batch, but right now I'm using the methods described here, with modifications: String replacement in batch file
Here's my code sample, but it doesn't work at all with ! or : in the strings:
:: NOT WORKING
:RemoveWordsFromFileInLine
:: %1 string to remove
:: %2 source file
:: %3 destination file
setlocal enabledelayedexpansion
:: Loop through each "word" of every "line"
set "toReplace=%1 "
set "blank="
del %3
for /F "tokens=* delims=" %%a in (%2) do (
setlocal disabledelayedexpansion
echo "line=%%a"
echo line is %line%
setlocal enabledelayedexpansion
set "newLine=!line:%toReplace%=%blank%!"
echo newLine is !newLine!
@echo !newLine! >>%3
endlocal
)
if exist %3 ( move /y %3 %2 >nul )
GOTO:EOF
As you can see, I've tried disabling delayed expansion on the "set" of my line variable, but I can't do that in the replacement section itself.
Some example strings in the file are:
<!-- XML Comment goes here -->
<planets> Mars world:Earth </planets>
<planets> Venus Earth Mercury </planets>
Some example strings I'd look to replace are:
world:Earth
Earth
Anyone have any ideas on how I could make the string replacement work? I've tried numerous things, usually involving putting double quotes in various places (like around the line variable in the string replace statement), but I haven't had any luck yet.