Computing.Net > Forums > Programming > Eval a batch var whose value is a set command

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Eval a batch var whose value is a set command

Reply to Message Icon

Name: tvattima
Date: October 27, 2009 at 07:10:03 Pacific
OS: Windows XP
Product: Microsoft Windows server 2003 enterprise
Subcategory: Batch
Comment:

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]



Sponsored Link
Ads by Google

Response Number 1
Name: klint
Date: October 27, 2009 at 10:14:59 Pacific
Reply:

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%


0

Response Number 2
Name: klint
Date: October 27, 2009 at 10:17:25 Pacific
Reply:

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


0

Response Number 3
Name: tvattima
Date: October 27, 2009 at 10:20:04 Pacific
Reply:

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?


0

Response Number 4
Name: klint
Date: October 27, 2009 at 10:59:11 Pacific
Reply:

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.


0

Response Number 5
Name: tvattima
Date: October 27, 2009 at 11:04:55 Pacific
Reply:

Thanks Clint!


0

Related Posts

See More



Response Number 6
Name: Judago
Date: October 27, 2009 at 14:23:14 Pacific
Reply:

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


1

Response Number 7
Name: klint
Date: October 28, 2009 at 04:07:07 Pacific
Reply:

Hi Judago,

Yes, your technique works!


0

Response Number 8
Name: Mechanix2Go
Date: October 28, 2009 at 05:59:33 Pacific
Reply:

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


0

Response Number 9
Name: tvattima
Date: October 28, 2009 at 06:04:21 Pacific
Reply:

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


0

Response Number 10
Name: Mechanix2Go
Date: October 28, 2009 at 06:10:06 Pacific
Reply:

Did Judago's reply in that thread get you going?


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

M2


0

Response Number 11
Name: tvattima
Date: October 28, 2009 at 06:21:44 Pacific
Reply:

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
)


0

Response Number 12
Name: Mechanix2Go
Date: October 28, 2009 at 06:35:02 Pacific
Reply:

What's in %JAVA_CMD%" ?


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

M2


0

Response Number 13
Name: Judago
Date: October 28, 2009 at 06:40:04 Pacific
Reply:

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


0

Response Number 14
Name: Judago
Date: October 28, 2009 at 06:51:00 Pacific
Reply:

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


0

Response Number 15
Name: Mechanix2Go
Date: October 28, 2009 at 07:07:01 Pacific
Reply:

"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


0

Response Number 16
Name: tvattima
Date: October 28, 2009 at 07:13:50 Pacific
Reply:

Ok folks, I'm going back to revisit this with the !ERRORLEVEL! approach!

Stay tuned. Appreciate all the expertise flying around here!


0

Response Number 17
Name: Mechanix2Go
Date: October 28, 2009 at 07:22:56 Pacific
Reply:

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


0

Response Number 18
Name: Judago
Date: October 28, 2009 at 08:02:05 Pacific
Reply:

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


0

Response Number 19
Name: Mechanix2Go
Date: October 28, 2009 at 08:19:02 Pacific
Reply:

LOL I don't do license xfer.


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

M2


0

Response Number 20
Name: klint
Date: October 28, 2009 at 08:22:23 Pacific
Reply:

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


0

Response Number 21
Name: Mechanix2Go
Date: October 28, 2009 at 08:32:26 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Eval a batch var whose value is a set command

DOS commands in a batch file www.computing.net/answers/programming/dos-commands-in-a-batch-file/16130.html

Newbie Question www.computing.net/answers/programming/newbie-question/2041.html

Detect 'Enter' using SET command? www.computing.net/answers/programming/detect-enter-using-set-command/17365.html