The content of a variable %myvar%
was set by the command SET myvar="aaa<bbb"
and this constitutes the input to the problem (a "given" that cannot be changed).
How to display the contents of the variable %myvar%
without surrounding quotes ?
Constraints: Do not write to the file system and do not use the for
command and delayed expansion.
I unsuccessfully tried the following:
echo %myvar:"=%
call echo %%myvar:"=%%
call echo %%myvar:"=^^<%%
set tmp=%myvar:"=%
call echo ^^^%tmp:<=^^^<^^^%
call set stripped=%%myvar:"=%%
call echo ^^^%stripped:^<=^^^<%%%
Just to prove that it is possible to display a redirection symbol <
without quotes and without delayed expansion, run the code below:
set myvar=aaa^^^<bbb
echo %myvar%
The code above is not a solution to the original problem because it changes the "given" input.
However, since the code above worked well for a very similar input (yet different), I attempted to set the contents of the variable %ttt% to "aaa^^^<bbb"
using the code below:
set ttt=%myvar:<=^^^<%
echo %ttt%
This displayed the text "aaa^^^<bbb"
, surrounded by quotes as expected.
Now, if we could just strip the quotes from the %ttt%
then we'd be home, ...but after trying to strip them with %ttt:"=%
and %ttt:~1,-1%
it still fails. For example:
echo "%ttt:"=%"
displays "aaa^^^<bbb"
proving that the quotes were successfully stripped (and later added on by the echo
).
echo %ttt:"=%
displays aaa^<bbb
without the quotes but it is not the sought after aaa<bbb
Edit:
After some more experimentation, I found the code that generates the desired output without the need to write any files to the file system, delayed expansion and without a for
loop. See my answer to my own question below...
can be easily done with delayed expansion:
@ECHO off
setlocal enabledelayedexpansion
SET myvar="aaa<bbb"
echo direct unquoted: !myvar:"=!
REM using a new variable for unquoted string:
set unquoted=!myvar:"=!
echo var unquoted: !unquoted!
Edit after additional contstraints ("no delayed expansion"), escaping poison char(s)
(using a subroutine to remove quotes; additional set
commands possible for additional possible poison chars)
@ECHO off
setlocal
set myvar="aaa<bbb"
call :print %myvar%
goto :eof
:print
set "unq=%~1"
set "unq=%unq:<=^<%"
echo %unq%