variable not getting update inside if condition in batch script

前端 未结 3 1135
再見小時候
再見小時候 2020-12-04 03:55
  @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 Q         


        
相关标签:
3条回答
  • 2020-12-04 04:24

    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
    
    0 讨论(0)
  • 2020-12-04 04:30

    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
    
    0 讨论(0)
  • 2020-12-04 04:36

    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
    
    0 讨论(0)
提交回复
热议问题