Escaped Characters and Variables

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
Apr 28, 2017
1
0
#1
I'm using TCC 19.10.54 x64 in Windows 7 Pro 64bit to run a batch file that accepts two arguments from the command line. The second argument contains escaped characters. In the batch, I'm assigning the second argument to a variable. I'm having problems with the variable displaying the correct output. The variable should display the same output as the command line argument2, however the variable displays different output. Below is the batch file code and the actual and expected output. Also, I tried this with TCC 21.00.20 x64 and get the same output. Is this the incorrect way to do this, a bug, or this cannot be done this way?

Code:
@echo off

rem Print the command line argument1 value.
echo cmdline arg1 value is: %1

rem Print the command line argument2 value.
echo cmdline arg2 value is: %2

rem Assign the command line argument2 value to a variable.
set arg2=%2

rem Print the variable containing the command line argument2 value.
echo variable arg2 value is: %arg2

quit
Actual Output
--------------
[C:\Users\testuser\AppData\Local\Temp]bf.bat x @^%^%$#
cmdline arg1 value is: x
cmdline arg2 value is: @%$#
variable arg2 value is: @x @%$##

Excepted Output
----------------
[C:\Users\testuser\AppData\Local\Temp]bf.bat x @^%^%$#
cmdline arg1 value is: x
cmdline arg2 value is: @%$#
variable arg2 value is: @%$#

Thanks.
 
#2
The problem is that when you echo %arg2, the %$ is interpreted at the whole set of args to the batch file (see "Batch File Parameters"). I would have expected "SETDOS /X-2" to prevent that but it doesn't.
 
#3
As I said before. %$ is a problem (meaning all the batfile's arguments). I was using the wrong SETDOS syntax. SETDOS /x-4 works.
Code:
v:\> t.btm x @^%^%$#
cmdline arg1 value is: x
cmdline arg2 value is: @%$#
arg2 is @%$#
variable arg2 value is: @%$#

v:\> type t.btm

rem Print the command line argument1 value.
echo cmdline arg1 value is: %1

rem Print the command line argument2 value.
echo cmdline arg2 value is: %2

rem Assign the command line argument2 value to a variable.
set arg2=%2

setdos /x-4
rem Print the variable containing the command line argument2 value.
echo variable arg2 value is: %[arg2]
setdos /x+4


v:\> t.btm x @^%^%$#
cmdline arg1 value is: x
cmdline arg2 value is: @%$#
variable arg2 value is: @%$#