IF, @WMI, parsing?

May 20, 2008
12,178
133
Syracuse, NY, USA
I can echo this expression.

Code:
v:\> echo "%@wmi[.,"select processid from Win32_service where name='spooler'"]"
"2536"

But I can't use it in a conditional expression.

Code:
v:\> if "%@wmi[.,"select processid from Win32_service where name='spooler'"]" ne "" echo OK
TCC: Syntax error "@wmi[.,"select"
Usage : IF [/I] [NOT] condition [.AND. | .OR. | .XOR. [NOT] condition ...] command

What's up with that?
 
May 20, 2008
12,178
133
Syracuse, NY, USA
IF is breaking the first argument at the space following select, then trying to expand that argument (which fails because it's a fragment of a function).

Put the "select ..." expression in a variable, then substitute that var in @wmi.
Is that WAD?

It's easier to use () instead of "".

Code:
v:\> if (%@wmi[.,"select processid from Win32_service where name='spooler'"]) ne () echo OK
OK
 
May 20, 2008
12,178
133
Syracuse, NY, USA
At a guess, the nested double-quotes are confusing IF's parser.
Nested quotes (inner ones in a function) are generally OK (or it would have surfaced a long time ago).

Code:
v:\> if "%@len["foo"]" == "5" echo yes
yes

It's the space! I wish I understood. I thought the function would guard whatever's in it.

Code:
v:\> if "%@len["foo "]" == "5" echo yes
TCC: Syntax error "@len["foo"
Usage : IF [/I] [NOT] condition [.AND. | .OR. | .XOR. [NOT] condition ...] command
 

rconn

Administrator
Staff member
May 14, 2008
12,557
167
WAD. IF doesn't know you're using a function with embedded quotes & whitespace; the [ and ] are not quote chars and don't protect embedded spaces within them. (TCC cannot treat them as quote chars unless you can convince everybody to not use them as filename characters.)
 
May 20, 2008
12,178
133
Syracuse, NY, USA
Hmmm! The habit of "double-quoting" the operands of a relational operator when one of them could be empty goes way, way back. I'd bet countless examples of it have appeared in the various support media over the years. I'm surprised I haven't run into this often enough that I'd remember it. In any event, other, more innocuous characters, ( (), {}, **, ... ) work just fine. And (a bit surprisingly) even [] works.

Code:
v:\> if [%@wmi[.,"select processid from Win32_service where name='spooler'"]] eq [2536] echo OK
OK
 

Similar threads