Home > Blockchain >  Windows Batch: How do you call a variable with concentric variable names and exclamation marks?
Windows Batch: How do you call a variable with concentric variable names and exclamation marks?

Time:11-11

I would like to call variables that contain other variables in their name when I have enabledelayedexpension so I would have concentric exclamation points in my variable call. I apologize for the unclear wording; I'm not very familiar with this subject.

Here's a portion of my code that shows my issue:

set /a Freq[%%z]value[!value!] =%%y
echo Freq %%z value !value! is !Freq[%%z]value[!value!]!

As is, batch interprets !Freq[%%z]value[!value!]! broken up into different variables with the !'s, and I don't think I can use %'s instead because I'm in a for loop where this variable is changing. I don't think I can use another for loop to replace !value! with a %%a either.

Here's a more complete look at my code:

set /a line=0
set /a goodLine=0
FOR /F "tokens=* delims=" %%x in (%file%) DO (
    set /a line =1
    echo Line is !line!
    set data[!line!]=%%x
    
    set /a value=0
    set /a checkGood=0
    FOR %%y in (%%x) DO (
        set /a value =1
        
        if !value!==1 (
            set /a Freq=%%y
            set /a checkFreq=%%y %% 10
            if !checkFreq!==0 (
                set /a checkGood=1
            ) else (echo bad)
        ) else (
            if !checkGood!==1 (
                for /l %%z in (40, 10, 330) do (
                    if !Freq!==%%z (
                      set /a Freq[%%z]value[!value!] =%%y
                      echo Freq %%z value !value! is !Freq[%%z]value[!value!]!
                      set /a Freq[%%z]quantity[!value!] =1
                      echo Freq %%z value !value! quantity is !Freq[%%z]quantity[!value!]!
                    )
                )
            ) else (echo checkGood bad)
        )
    )
)

CodePudding user response:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET /a value=4
FOR /L %%z IN (1,1,5) DO set /a Freq[%%z]value[!value!]=%%z 10

SET Freq
ECHO ========================

FOR %%y IN (2,7) DO FOR /L %%z IN (1,1,5) DO (
 set /a Freq[%%z]value[!value!] =%%y
 SET Freq[%%z]value[!value!]
 CALL echo Freq %%z value !value! is %%Freq[%%z]value[!value!]%%
)

GOTO :EOF

Or you could use a for/f "tokens=2delims==" %%e in ('set Freq[%%z]value[!value!]') do echo %%e

depends on what you really want to do.

  • Related