Why the string with question mark is not printed (from command prompt)?
A code example:
FOR %A IN (--eval string?) DO ECHO %A
The code will print only --eval
but ignores string?
Is there a workaround to get it printed?
I have tried to:
- enclose it in double quotes
"string?"
- tried to escape it
"string^?"
or"string^^^?"
like with bang!
- tried to double the
??
- tried combination of these
Nothing seems to work. This seems to be a bug.
Edit base on the answer and question at comment.
What do I expect? I would expect having both options --eval string? printed.
What I'm trying to achieve? I tried to give a simplification of my final goal.
In the end, I wish to have both strings in the !temp_string!
variable (in the below test.bat
example).
An example based on the real code:
test.bat
example:
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
ECHO "First argument outside FOR statement: %~1"
ECHO "Second argument outside FOR statement: %~2"
FOR %%A IN (%*) DO (
ECHO "First inside FOR argument: %~1"
ECHO "Second inside FOR argument: %~2"
SET "temp_string=%%~A"
ECHO !temp_string!
)
If you execute the text.bat without the ?
in question like:
test.bat --eval --test
it will correctly show the both strings.
The result:
"First argument outside FOR statement: --eval"
"Second argument outside FOR statement: --test"
"First inside FOR argument: --eval"
"Second inside FOR argument: --test"
--eval
"First inside FOR argument: --eval"
"Second inside FOR argument: --test"
--test
However, if you run it with the same parameter, but containing ?
test.bat --eval --test?
The result is incorrect:
"First argument outside FOR statement: --eval"
"Second argument outside FOR statement: --test?"
"First inside FOR argument: --eval"
"Second inside FOR argument: --test?"
--eval
If you wish to see the real code, which is here simplified, you can find it here around line 783.
Second edit
I'm now trying to get the value of the %%A
from the proposed solution by @dbenham.
The %test%
variable is still empty in both cases:
@echo off
setlocal disableDelayedExpansion
set "string=--eval string? ;hello <&|>! "^<^&^|^>!""
set string
echo(
setlocal enableDelayedExpansion
for %%n in (^"^
%= This results in a quoted newline character =%
^") do for /f delims^=^ eol^= %%A in ("!string: =%%~n!") do (
if "!!" equ "" endlocal set "test=%%~A"
echo "A value:" %%A
echo "Test value:" %test%
)
echo %test%
The output:
string=--eval string? ;hello <&|>! "<&|>!"
"A value:" --eval
"Test value:"
"A value:" string?
"Test value:"
"A value:" ;hello
"Test value:"
"A value:" <&|>!
"Test value:"
"A value:" "<&|>!"
"Test value:"
ECHO is off.
Third and Forth Edit - The answer.
Based the dbenham && Stephan I'm posting working code with which I'm satisfied.
Note: The dbenham's nasty "^<^&^|^>!"
are not working here, but I decided I don't needed it.
@echo off
REM works for all!!! --> *.bat -string? :hello "<&|>!"
setlocal disableDelayedExpansion
:process_arguments
:: Process all arguments in the order received
if defined %1 (
ECHO %1
SET "string=%string% %~1"
shift
goto:process_arguments
)
echo(
setlocal enableDelayedExpansion
for %%n in (^"^
%= This results in a quoted newline character =%
^") do for /f "eol= delims=" %%A in ("!string: =%%~n!") do (
if "!!" equ "" endlocal
set "test=%%~A"
echo "In for loop - a value: %%A"
CALL :testing_value "%%A"
)
:: END
goto :eof
:: echo "Outside test:" %test%
setlocal disableDelayedExpansion
:testing_value
set "test=%~1"
set test
echo "Calling Subroutine: %test%"
echo:
Thank you your guidance!