You are not logged in.

#1 23 Apr 2007 16:43

blah01
Member
Registered: 23 Apr 2007
Posts: 2

problem with a variable inside for in a cmd script

hi all

At the begining i want to say that I'm new to the cmd scripts.
I have a problem with a variable inside a for loop. I'm reading data from a file:

season=spring summer autumn winter
month=january february march april may

This is a script code (it's only a test script):

@echo off

FOR /F "eol=# tokens=1,2 delims==" %%i in (%1) do  (
    if %%i == season SET SEASON=%%j
)

FOR /F "eol=# tokens=1,2 delims==" %%i in (%1) do  (
    if %%i == month SET MONTH=%%j
)

for %%i in ( %SEASON% ) DO (
      for %%j in ( %MONTH% ) do (
        set x=%%i\%%j
        REM echo %%i\%%j
        echo %x%
      )
)

The problem is that the x variable has at every iterance the same value:
winter\may
winter\may
winter\may
winter\may
winter\may
winter\may
winter\may
winter\may
winter\may
winter\may
winter\may
winter\may
winter\may
winter\may
winter\may
winter\may
winter\may
winter\may
winter\may
winter\may

While the commented echo %%i\%%j gives the correct result:
spring\january
spring\february
spring\march
spring\april
spring\may
summer\january
summer\february
summer\march
summer\april
summer\may
autumn\january
autumn\february
autumn\march
autumn\april
autumn\may
winter\january
winter\february
winter\march
winter\april
winter\may

Does anyone knows what is the problem?

blah01

Offline

#2 23 Apr 2007 18:18

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: problem with a variable inside for in a cmd script

You need to SETLOCAL EnableDelayedExpansion

see the SETLOCAL page
http://ss64.com/nt/setlocal.html

Offline

#3 24 Apr 2007 08:30

blah01
Member
Registered: 23 Apr 2007
Posts: 2

Re: problem with a variable inside for in a cmd script

thx this has solved my problem smile

Offline

Board footer

Powered by