Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have a need to evaluate a batch script variable whose value will be something like this:
set cmd="set a=123&set b=456&c=xyz"
Here is my script:@echo off
set cmd="set a=123&set b=456&c=xyz"REM What do I do here to invoke the %cmd% variable as a command that will yeild me the three new environment variables a,b and c being set?
echo a=[%a%]
echo b=[%b%]
echo c=[%b%]So at the send, I would see echoed on the screen
a=[123]
b=[456]
c=[xyz]

Do you really need the quotes inside the value of cmd? If you leave out the quotes, you can just run the command using:
%cmd%

By the way, if you don't have quotes in the set command, you'll have to escape the special characters such as &. So:
set cmd=set a=123^&set b=456^&set c=xyz

hey clint,
I observed without the double quotes, that this command:
set cmd=set a=123&set b=456&c=xyzshort-circuits %cmd% to have a value of: set a=123
where everything after the first "&" is gone!(Note I had an error in the original post as the c=xyz should be set c=xyz.)
So again, the general question is if the cmd variable has a value that is any number of chained set commands, each separated by the ampersand, how does one invoke that so that those chained set commands are executed?

See my second reply.
The problem is that the Command Processor takes & as a statement separator, so
set cmd=set a=123&set b=456&set c=xyzis interpreted as:
set cmd=set a=123 set b=456 set c=xyzBut if you use the escape character (^) before the & then you get the desired effect:
set cmd=set a=123^&set b=456^&set c=xyz echo "%cmd%"Gives: "set a=123&set b=456&set c=xyz"
and just entering %cmd% on a line makes it execute cmd.

I believe there is another way to achieve this without the escape character:
set "cmd=set a=123&set b=456&set c=xyz" %cmd%^^^ note the position of the first quote.
I can't test it at the moment because I don't have a working windows install(long story...), but I'm sure it works.

Besides the obvious, that using an existing command or reserved word for a varianle is uasually a bad idea, I used this:
set "var=set a=123&set b=456&set c=xyz"
and wound up with:
var=set a=123&set b=456&set c=xyz
a=123&set b=456&set c=xyzno b and no c
So I guess we're stuck escaping the &.
=====================================
Helping others achieve escape felicityM2

You guys are awesome thanks! We can consider this one solved.
But for another challenge, please checkout my other, bigger issue over here:
http://www.computing.net/answers/pr...

Did Judago's reply in that thread get you going?
=====================================
Helping others achieve escape felicityM2

No because my problem is not simply a matter of knowing there was an error, I really need the actual value.
for /F "usebackq delims=€" %%G in (`"%JAVA_CMD%"`) do (
if ERRORLEVEL 1 (
REM ---> I need the actual value here...1, 2, 3, etc...set into _rc
)
set _stdout=%%G
)

What's in %JAVA_CMD%" ?
=====================================
Helping others achieve escape felicityM2

Hi M2,
I had a vague thought about the reserved var %cmd% but couldn't quite remember(haven't been bothered to install + call india yet...).
Good catch!
Hi Klint,
I was pretty sure it would, just been tripped up too many times....
@tvattima
Well there are a couple of ways to achieve this (the link to the how to in my sig explains in much more detail).
In most cases all you will have to do is:
setlocal enabledelayedexpansion for /F "usebackq delims=€" %%G in (`"%JAVA_CMD%"`) do ( rem do your thing with !errorlevel! set _stdout=%%G )Just be aware that !'s will dissapear from your output, if they are important there is a couple of ways to get around that too...

M2,
I just re-read your post and noticed:
"I used this:
set "var=set a=123&set b=456&set c=xyz"
and wound up with:
var=set a=123&set b=456&set c=xyz
a=123&set b=456&set c=xyzno b and no c
So I guess we're stuck escaping the &.
I haven't seen this behaviour before, perhaps the quote trick was introduced with xp or something, I guess it may be one of those things to watch out for with win2k.

"haven't been bothered to install + call india yet"
I better stand up; they're goin' over my head.
I had a real queasy feeling about the backq and the mystery delim so I wrote this:
===============================
for /F "tokens=* delims= " %%G in ('chkdsk d:') do (
echo !ERRORLEVEL!
set _stdout=%%G
)
set > newfile
===========================
Which spits out zeros and sets:_stdout=77,431 allocation units available on disk.
which is just what I'd expect.
If I wanted a var set to the exit code I would:
===========================
chkdsk z:
echo !ERRORLEVEL!
set _stdout=!ERRORLEVEL!
set > newfile
========================
I get:_stdout=3
because I don't HAVE a z: drive.
=====================================
Helping others achieve escape felicityM2

Ok folks, I'm going back to revisit this with the !ERRORLEVEL! approach!
Stay tuned. Appreciate all the expertise flying around here!

Hi Judago,
Yeah, could be a 2k/XP gotcha.
Here's the crc for w2k SP4 cmd.exe
4E0D158A
=======================
tvattima,Changing from % to ! didn't help. Getting it out of the for loop did.
=====================================
Helping others achieve escape felicityM2

I better stand up; they're goin' over my head.
I'm at slight disadvantage today I must admit. I had a motherboard die and am running ubuntu at the moment. That means no testing.
I normally run xp: dead mobo = Re-install/repair windows + call india(via ms) to tell some guy to transfer my retail windows license to new hardware :(
I should probably just bow out but I think this forum is addictive or something.....

LOL I don't do license xfer.
=====================================
Helping others achieve escape felicityM2

@M2: What's wrong with using cmd as a variable name? Where is it reserved? On Windows XP and Vista, there is no reserved variable by the name of CMD.

Hi klint,
I respect your ability and don't want to start a debate.
I can't count the times some noob has, for instance, created a bat to ftp and cleverly named it ftp.bat then couldn't figure out why it blew up.
=====================================
Helping others achieve escape felicityM2

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |