views:

153

answers:

2

Hi,

I am having trouble with the following command prompt commands (in Windows XP).

    set SOMEVAR=
    for /F %i in (1 2 3) do set SOMEVAR=%SOMEVAR% "%i"
    echo %SOMEVAR%

I expect it to build the SOMEVAR variable so that it contains each item in the for loop in quotes, separated by a space:  1 2 3

However what this is what I get instead.

  >    set SOMEVAR=
  >    for /F %i in (1 2 3) do set SOMEVAR=%SOMEVAR% "%i"
  >set SOMEVAR=%SOMEVAR% "1"
  >set SOMEVAR=%SOMEVAR% "2"
  >set SOMEVAR=%SOMEVAR% "3"
  >    echo %SOMEVAR%
  %SOMEVAR% "3"

It looks like environment variables are not updated and/or expanded during a FOR loop.

Any ideas how to build an environment variable with a FOR loop?

A workaround that I’m currently using is to have the FOR loop call a local label in the BAT file which SETs the variable to itself plus %1, then jumps to :EOF. It works, but I’d like to figure out if there is a way to get it to work in one line without the call and label overhead.

+1  A: 

its an option you have to enable

> help for

will explain

oops, i meant

> help set

be sure to read all the way to the bottom

Edit: it turns out that you can turn this on in an individual a batch file. save this text as temp.bat

SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
set SOMEVAR=
for %%i IN (temp.*) DO set SOMEVAR=!SOMEVAR! "%%i"
echo %SOMEVAR%
John Knoeller
I’ve tried the delayed expansion—! instead of %—detailed in SET /?, but it doesn’t work, it just does the same thing. (Command Extensions *are* enabled.)
Synetech inc.
pay careful attention to the text of help set. Delayed expansion is not enabled by default _even when command extensions are enabled_turning it on is a separate flag.
John Knoeller
Ah, I see. It’s a switch to CMD, not the SET command. That’s going to require altering the command prompt’s command line itself. Hmm, now I have to decide if it’s worth the extra trouble. BRB…
Synetech inc.
Okay, I tried it and it doesn’t work correctly. The environment variable does get built, but then it has the variable name as part of the list. IE, using the example given in SET/?: **for %i in (\*) do set LIST=!LIST! %i**, the result ends up starting with !LIST!
Synetech inc.
Try running the little batch script that I put in my post. (make sure that you have a least 2 files named temp. When I run that batch on my machine, I end up with `SOMEVAR = "temp.bat" "temp.bak"`
John Knoeller
Cool, that worked correctly, and by using the SETLOCAL command, I don’t have to invoke a separate command prompt.Thanks a lot.
Synetech inc.
A: 

here's an equivalent alternative, using vbscript which is already on your system

somevar=""
For i=1 To 3 
    somevar=somevar & i
Next
WScript.Echo somevar

output

C:\test>cscript //nologo test.vbs 123

Plus, if you are going to use a lot of these stuff, you can make use of vbscript's dictionary collection or arrays to store your variables.

ghostdog74
Thanks, but VBScript isn’t going to work for this, I am using a batch file to set an *environment* variable, which is then used to perform some commands.
Synetech inc.
vbscript can be used to set environment variable as well.
ghostdog74