Eval a batch var whose value is a set command

Microsoft Windows server 2003 enterprise
October 27, 2009 at 07:10:03
Specs: Windows XP
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]


See More: Eval a batch var whose value is a set command

Report •


#1
October 27, 2009 at 10:14:59
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%


Report •

#2
October 27, 2009 at 10:17:25
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


Report •

#3
October 27, 2009 at 10:20:04
hey clint,
I observed without the double quotes, that this command:
set cmd=set a=123&set b=456&c=xyz

short-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?


Report •

Related Solutions

#4
October 27, 2009 at 10:59:11
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=xyz

is interpreted as:

   set cmd=set a=123
   set b=456
   set c=xyz

But 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.


Report •

#5
October 27, 2009 at 11:04:55
Thanks Clint!

Report •

#6
October 27, 2009 at 14:23:14
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.


Batch Variable how to


Report •

#7
October 28, 2009 at 04:07:07
Hi Judago,

Yes, your technique works!


Report •

#8
October 28, 2009 at 05:59:33
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=xyz

no b and no c

So I guess we're stuck escaping the &.


=====================================
Helping others achieve escape felicity

M2


Report •

#9
October 28, 2009 at 06:04:21
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...


Report •

#10
October 28, 2009 at 06:10:06
Did Judago's reply in that thread get you going?


=====================================
Helping others achieve escape felicity

M2


Report •

#11
October 28, 2009 at 06:21:44
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
)


Report •

#12
October 28, 2009 at 06:35:02
What's in %JAVA_CMD%" ?


=====================================
Helping others achieve escape felicity

M2


Report •

#13
October 28, 2009 at 06:40:04
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...


Batch Variable how to


Report •

#14
October 28, 2009 at 06:51:00
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=xyz

no 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.


Batch Variable how to


Report •

#15
October 28, 2009 at 07:07:01
"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 felicity

M2


Report •

#16
October 28, 2009 at 07:13:50
Ok folks, I'm going back to revisit this with the !ERRORLEVEL! approach!

Stay tuned. Appreciate all the expertise flying around here!


Report •

#17
October 28, 2009 at 07:22:56
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 felicity

M2


Report •

#18
October 28, 2009 at 08:02:05
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.....


Batch Variable how to


Report •

#19
October 28, 2009 at 08:19:02
LOL I don't do license xfer.


=====================================
Helping others achieve escape felicity

M2


Report •

#20
October 28, 2009 at 08:22:23
@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.

Report •

#21
October 28, 2009 at 08:32:26
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 felicity

M2


Report •


Ask Question