2

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.

Synetech
  • 9,643
  • 9
  • 64
  • 96
  • checkout the edit in my answer below. this worked on my machine when I used both SETLOCAL xxx statements, but not otherwise. Win7 – John Knoeller Dec 23 '09 at 17:33
  • No you don't have to change the switches to cmd, you can override buy putting SETLOCAL statements at the top of your batch file. – John Knoeller Dec 23 '09 at 18:41

2 Answers2

3

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
  • 33,512
  • 4
  • 61
  • 92
  • 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 Dec 23 '09 at 05:50
  • 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 Dec 23 '09 at 06:49
  • 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 Dec 23 '09 at 18:01
  • 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 Dec 23 '09 at 18:04
  • 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 Dec 23 '09 at 18:29
  • Cool, that worked correctly, and by using the SETLOCAL command, I don’t have to invoke a separate command prompt. Thanks a lot. – Synetech Dec 23 '09 at 19:10
  • On some reason the accumulation `set SOMEVAR=!SOMEVAR! "%%i"` does not work for me whatever I tried (`cmd /v:on` or `setlocal enabledelayedexpansion`): the variable `!SOMEVAR!` was left unsubstituted. The solution is to call a function and in the function do the accumulation, see [stackoverflow post](http://superuser.com/a/288272/28311). – dma_k Jul 09 '14 at 14:24
0

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
  • 327,991
  • 56
  • 259
  • 343
  • 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 Dec 23 '09 at 18:05
  • vbscript can be used to set environment variable as well. – ghostdog74 Dec 24 '09 at 00:01