Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

IF, @WMI, parsing?

May
12,798
160
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?
 
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
 
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
 
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

Back
Top