Handling of %~I problem

Nov 6, 2014
26
0
cmd supports this bit of variable expansion in a `for` loop (from the `for /?` help):

Code:
 %~I         - expands %I removing any surrounding quotes (")

tcc used to handle this construct the same way as cmd until v21. Here's a simple test script:

Code:
C:\Program Files\JPSoft>type c:\temp\test.cmd
@echo off
for /f "usebackq" %%a IN ('test') do (
    echo a is :%%a:
    echo ~a is :%%~a:
)

for /f "usebackq" %%a IN ('"test2"') do (
    echo a is :%%a:
    echo ~a is :%%~a:
)

cmd and tcc through v20 process it the same:

Code:
C:\Program Files\JPSoft>ver

Microsoft Windows [Version 10.0.17134.765]

C:\Program Files\JPSoft>c:\temp\test.cmd
a is :test:
~a is :test:
a is :"test2":
~a is :test2:

C:\Program Files\JPSoft>TCMD20\tcc c:\temp\test.cmd

TCC  20.11.46 x64   Windows 10 [Version 6.3.17134]
Copyright 2017 JP Software Inc.  All Rights Reserved
Your evaluation period expires in 30 days.
You can buy Take Command and TCC at https://jpsoft.com

a is :test:
~a is :test:
a is :"test2":
~a is :test2:

But starting with tcc v21 the `%~I` construct expands to nothing:

Code:
C:\Program Files\JPSoft>TCC_RT_21\tcc c:\temp\test.cmd

TCC-RT  21.01.63 x64   Windows 10 [Version 10.0.17134]
Copyright 2017 JP Software Inc.  All Rights Reserved
a is :test:
~a is ::
a is :"test2":
~a is ::
 

rconn

Administrator
Staff member
May 14, 2008
12,340
149
The problem is that TCC thinks that any of the characters A D F N P S T X Z following a ~ is a special CMD FOR argument. In this case, the unfortunate use of A as the FOR variable confused TCC (since ~A means "display the file attributes"). If you use some other character (i.e., B C E G etc.) it will work in both TCC and CMD.

I will try to come up with a workaround for the next version, but this particular syntax is complicated as I'm already kludging around two CMD bugs.
 
Nov 6, 2014
26
0
Thanks. I hope you can come up with a workaround - I hit this with a third party script and have no idea if the author would accept a change request based on tcc compatibility. I can make the change locally easily enough though so it's certainly not an emergency.

Now that I look a little more closely at the cmd %~ modifier syntax I can see how it would be pretty painful to parse. I imagine you already know this, but I'll post what I concluded from 5 minutes of playing with cmd.exe's ~% expansion:

It looks like when cmd sees the %~modifier syntax, it does something like:

- as long as the next character is in the set [adfnpstxz] (upper or lower case) or is the FOR variable name (must match the FOR variable's case), push it on a stack
- when you reach a character that doesn't meet the above criteria, stop pushing
- pop characters off the stack until you reach the FOR variable

Now you know which is the FOR variable, and all the other characters on the stack are modifiers (repeated modifiers are ignored).

I haven't thought about how to fit the %~$PATH:I construct into that scheme.
 
Nov 6, 2014
26
0
When updating the script, I found that the likely reason that "A" was used as the FOR variable is because it makes it a bit easier to follow what token is going where when the FOR statement parses several tokens into sequential variables:

Code:
for /f "usebackq tokens=1,2,*" %%a in ('first second third') do (
    echo 1: %%a
    echo 2: %%b
    echo 3: %%c
)

Regardless, the workaround is in place here. Thanks.
 
Aug 23, 2010
636
9
The problem is that TCC thinks that any of the characters A D F N P S T X Z following a ~ is a special CMD FOR argument. In this case, the unfortunate use of A as the FOR variable confused TCC (since ~A means "display the file attributes"). If you use some other character (i.e., B C E G etc.) it will work in both TCC and CMD.

I will try to come up with a workaround for the next version, but this particular syntax is complicated as I'm already kludging around two CMD bugs.
CMD is greedy in this case. It takes
Code:
\~[fdpnxsatz]+[0-9a-zA-Z]\b
approach.
 
Similar threads
Thread starter Title Forum Replies Date
vefatica WAD TCC: inconsistent character handling Support 11
J File explorer handling from command line? Support 2
forbin Strange handling of [nonbright] magenta background (v22) Support 2
T WAD Difference between TCC and CMD in handling multi-command lines Support 6
thorntonpg Handling files with embedded spaces Support 1
J junction copy/move handling Support 6
C How to? MOVE and handling out of space Support 2
The Wizard WAD Possible Incorrect COMSPEC handling Support 2
BobK How to? Handling filenames with blanks at command line Support 3
Frank WAD handling variables with $-prefix Support 14
A Discrepancy in handling of "can't copy or move file to itself" Support 4
M How to? A question about error-handling... Support 25
S Description handling Support 3
B Strange handling of a .BAT file Support 5
C handling percent signs in filenames Support 2
C handling percent signs in filenames Support 1
S Ctrl-C handling while piping Support 0
vefatica Icon handling in b153 Support 7
U An annoying bug with TCC's icon handling in the taskbar Support 25
p.f.moore Bug in plugin break handling? Support 0
p.f.moore Error handling in plugins Support 2
Dick Johnson Weird Color Problem Support 8
fishman@panix.com Problem with 27.15 Support 2
M Problem with VSDevCmd.bat in VS 16.7.3 Support 0
R Problem with @INT[ value] in V26 Support 9
M Selecting test "off by one" problem in Take command Support 4
Alpengreis UTF-8 problem in TCC related to Python Support 7
K_Meinhard Small problem in german IDE 26 Support 3
B Problem with color in nested shells Support 1
Joe Caverly Problem creating and switching to a DESKTOP Support 9
vefatica Another popup problem Support 10
Alpengreis ffind dialog (/W) problem Support 4
Alpengreis [TCMD v25.00.24] Small space problem with the DE translation in Prefs-GUI Support 1
Alpengreis [TCMD v25.00.24] Problem with copy and paste and the # char via mouse in TCC Support 6
A Problem with functions @int @decimal and identifying Powershell as a shell. Support 12
B IF command problem in tcexit.btm Support 9
fishman@panix.com Problem at Startup of TCC Support 3
P Problem with SFTP copies Support 7
P Problem with FTP copies Support 10
Jay Sage Problem with Context Menu Copy+Paste+Run Key Assignment Support 7
R Problem with %_do_loop in nested do loops Support 2
fishman@panix.com New Problem with later Windows 10 Support 10
Peter Murschall A little problem with LEAVE and COMMENT Support 5
WinLanEm FOR problem Support 18
K Problem With SCRPUT /u Support 3
vefatica What's TCMD's problem with ^e[0m? Support 13
Peter Murschall IDE: RTL with non-English resources-problem is back in Build 28 Support 4
rps Regex problem: \xnn not recognized as a hex character Support 0
rps PRIORITY LOW problem Support 1
old coot Regex problem: \xnn not recognized as a hex character Support 12

Similar threads