0
  @echo off

  SET CONFIGS_QUASAR1=Q1_1 Q1_2 Q1_3 Q1_4 Q1_5 Q1_6
  SET CONFIGS_QUASAR2=Q2_1 Q2_2 Q2_3 Q2_4 Q2_5 Q2_6
  SET CONFIGS_QUASAR3=Q3_1 Q3_2 Q3_3 Q3_4 Q3_5 Q3_6
  SET CONFIGS_QUASAR0B=Q0B_1 Q0B_2 Q0B_3 Q0B_4 Q0B_5 Q0B_6

  FOR %%A IN (QUASAR1 QUASAR2 QUASAR3 QUASAR0B) DO (
    IF "%%A" == "QUASAR1" (
      SET CONFIGS=%CONFIGS_QUASAR1%
    ) ELSE IF "%%A" == "QUASAR2" (
      SET CONFIGS=%CONFIGS_QUASAR2%
    ) ELSE IF "%%A" == "QUASAR3" (
      SET CONFIGS=%CONFIGS_QUASAR3%
    ) ELSE IF "%%A" == "QUASAR0B" (
      SET CONFIGS=%CONFIGS_QUASAR0B%
    )

    echo %%A
    echo %CONFIGS%
  )

  pause

I'm very new to batch file programming. I have written a very small program to set a variable inside an 'if' condition, but the variable("CONFIGS") is not getting udated in the above mentioned program.

Please check the program and tell me what should be modified?

Output of the batch file:

QUASAR1
Q0B_1 Q0B_2 Q0B_3 Q0B_4 Q0B_5 Q0B_6
QUASAR2
Q0B_1 Q0B_2 Q0B_3 Q0B_4 Q0B_5 Q0B_6
QUASAR3
Q0B_1 Q0B_2 Q0B_3 Q0B_4 Q0B_5 Q0B_6
QUASAR0B
Q0B_1 Q0B_2 Q0B_3 Q0B_4 Q0B_5 Q0B_6

3 Answers 3

1

Excuse me. Although your problem is directly related to Delayed Expansion as npocmaka indicated in his answer, I would like to attract your attention to the following advice.

When you use Delayed Expansion you may make good use of the fact that the second (delayed) !expansion! may work over text that has been previously modified or %expanded%. This is a powerful Batch file feature that may avoid complex manipulations.

For example, suppose that you have a variable called result that will store the sum of another variable called term plus the values 1, 2, 3 and 11. One way to do that is this:

for %%a in (1 2 3 11) do (
   if %%a == 1 (
      set /A result=term+1
   ) else if %%a == 2 (
      set /A result=term+2
   ) else if %%a == 3 (
      set /A result=term+3
   ) else if %%a == 11 (
      set /A result=term+11
   )
   echo The sum of !term! plus %%a is !result!
)

However, you may conclude that all those IF's are not necessary, because you may directly take the value of the second term this way:

for %%a in (1 2 3 11) do (
   set /A result=term+%%a
   echo The sum of !term! plus %%a is !result!
)

Well, the same conclusion may be used in your code this way:

@echo off
setlocal EnableDelayedExpansion

rem Create CONFIGS_QUASAR 1, 2, 3 and 0B strings
FOR %%A IN (1 2 3 0B) DO (
   rem Initialize this string
   SET "CONFIGS_QUASAR%%A="
   rem Fill this string with their values
   FOR /L %%I IN (1,1,6) DO SET "CONFIGS_QUASAR%%A=!CONFIGS_QUASAR%%A! Q%%A_%%I"
)

FOR %%A IN (1 2 3 0B) DO (
   SET CONFIGS=!CONFIGS_QUASAR%%A!

   echo QUASAR%%A
   echo !CONFIGS!
)

pause

Output:

QUASAR1
 Q1_1 Q1_2 Q1_3 Q1_4 Q1_5 Q1_6
QUASAR2
 Q2_1 Q2_2 Q2_3 Q2_4 Q2_5 Q2_6
QUASAR3
 Q3_1 Q3_2 Q3_3 Q3_4 Q3_5 Q3_6
QUASAR0B
 Q0B_1 Q0B_2 Q0B_3 Q0B_4 Q0B_5 Q0B_6
Sign up to request clarification or add additional context in comments.

Comments

0

delayed expansion!!!

 @echo off

  SET CONFIGS_QUASAR1=Q1_1 Q1_2 Q1_3 Q1_4 Q1_5 Q1_6
  SET CONFIGS_QUASAR2=Q2_1 Q2_2 Q2_3 Q2_4 Q2_5 Q2_6
  SET CONFIGS_QUASAR3=Q3_1 Q3_2 Q3_3 Q3_4 Q3_5 Q3_6
  SET CONFIGS_QUASAR0B=Q0B_1 Q0B_2 Q0B_3 Q0B_4 Q0B_5 Q0B_6

  setlocal enableDelayedExpansion
  FOR %%A IN (QUASAR1 QUASAR2 QUASAR3 QUASAR0B) DO (
    IF "%%A" == "QUASAR1" (
      SET CONFIGS=CONFIGS_QUASAR1%
    ) 
    IF "%%A" == "QUASAR2" (
      SET CONFIGS=%CONFIGS_QUASAR2%
    ) 
    IF "%%A" == "QUASAR3" (
      SET CONFIGS=%CONFIGS_QUASAR3%
    ) 
    IF "%%A" == "QUASAR0B" (
      SET CONFIGS=%CONFIGS_QUASAR0B%
    )

    echo %%A
    echo !CONFIGS!
  )

  pause

3 Comments

QUASAR1 !CONFIGS! QUASAR2 !CONFIGS! QUASAR3 !CONFIGS! QUASAR0B !CONFIGS!
Im getting the above output. what should i do?
@JoeJoseph: You need to include SETLOCAL ENABLEDELAYEDEXPANSION command at beginning, just below @echo off.
0

Sorry for the above comment,

I got the answer, the batch file is working fine now. Please refer to the corrected code below. Thank you very much :)

    @echo off
    Setlocal EnableDelayedExpansion

    SET CONFIGS_QUASAR1=Q1_1 Q1_2 Q1_3 Q1_4 Q1_5 Q1_6
    SET CONFIGS_QUASAR2=Q2_1 Q2_2 Q2_3 Q2_4 Q2_5 Q2_6
    SET CONFIGS_QUASAR3=Q3_1 Q3_2 Q3_3 Q3_4 Q3_5 Q3_6
    SET CONFIGS_QUASAR0B=Q0B_1 Q0B_2 Q0B_3 Q0B_4 Q0B_5 Q0B_6

    FOR %%A IN (QUASAR1 QUASAR2 QUASAR3 QUASAR0B) DO (
      IF "%%A" == "QUASAR1" (
        SET CONFIGS=%CONFIGS_QUASAR1%
      ) ELSE IF "%%A" == "QUASAR2" (
        SET CONFIGS=%CONFIGS_QUASAR2%
      ) ELSE IF "%%A" == "QUASAR3" (
        SET CONFIGS=%CONFIGS_QUASAR3%
      ) ELSE IF "%%A" == "QUASAR0B" (
        SET CONFIGS=%CONFIGS_QUASAR0B%
      )

      echo %%A
      echo !CONFIGS!
    )

    pause

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.