I want to substitute angle brackets for commas in a variable.
I'm trying to use trick with for /f
for this, but it works only for echoing that variable.
This code works:
@ECHO OFF
set GSoption=" << >> "
for /f "tokens=1* delims==" %%a in ('set GSoption') do echo "%%b"
But this one doesn't:
@ECHO OFF
set GSoption=" << >> "
set "Newvar=, ,"
for /f "tokens=1* delims==" %%a in ('set GSoption') do call :check %%b
exit /b
:check
SET "_result=%%Newvar:,=%1%%"
echo|call set /p="%_result%">>log.txt
exit /b
goto :eof
It prints unexpected <<
I want to substitute those angle brackets for commas. Is it even possible without delayedexpansion?
Your question does not fully define your intention, however the solution is to use delayed expansion.
Here's a quick example replacing all <
and >
characters with ,
characters.
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "GSoption= << >> "
SetLocal EnableDelayedExpansion
Set "_Result=!GSOption:>=,!"
(
For %%G In ("!_Result:<=,!") Do (
EndLocal
Echo %%~G
)
) 1>"log.txt"
If you don't want a trailing CRLF, then use Set /P
instead:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "GSoption= << >> "
SetLocal EnableDelayedExpansion
Set "_Result=!GSOption:>=,!"
(
For %%G In ("!_Result:<=,!") Do (
EndLocal
Set /P "=%%~G" 0<NUL
)
) 1>"log.txt"
As you have not explained the purpose of your for
loop, you could of course do it without that too:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "GSoption= << >> "
SetLocal EnableDelayedExpansion
Set "_Result=!GSOption:>=,!"
(Echo(!_Result:^<=,!) 1>"log.txt"
EndLocal
And once again without the CRLF
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "GSoption= << >> "
SetLocal EnableDelayedExpansion
Set "_Result=!GSOption:>=,!"
Set /P "=!_Result:<=,!" 0<NUL 1>"log.txt"
EndLocal