Computing Staff
  • 0

Delayed Expansion And Substring Processing?

  • 0

Hi,
I’d like to create variable names where I can control the sort order of the values that the variables contain. I had hoped the following would facilitate such a desire:

 

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET /A position=0
SET order=DEFABC

FOR /L %%A IN (1,1,6) DO (
    SET sequence=%order:~!position!,1%
    SET some_variable_!sequence!=%%A
    SET /A position+=1
)

FOR /F "tokens=1* delims==" %%A IN ('SET some_variable_') DO (
    ECHO "%%A" = "%%B"
)

EXIT /B

Desired output:

"some_variable_A" = "4"
"some_variable_B" = "5"
"some_variable_C" = "6"
"some_variable_D" = "1"
"some_variable_E" = "2"
"some_variable_F" = "3"

Actual output:

"some_variable_order:~0,1" = "1"
"some_variable_order:~1,1" = "2"
"some_variable_order:~2,1" = "3"
"some_variable_order:~3,1" = "4"
"some_variable_order:~4,1" = "5"
"some_variable_order:~5,1" = "6"

What am I missing? You folks here are pretty sharp, and I was curious if there is an alternate method that would accomplish my goal.

*Edit – Fixed typo

Share

1 Answer

  1. You don’t even need to use call:
    @ECHO OFF
    SETLOCAL ENABLEDELAYEDEXPANSION

    SET /A position=0
    SET order=DEFABC

    FOR /L %%A IN (1,1,6) DO (
    for %%b in (!position!) do SET sequence=!order:~%%b,1!
    SET some_variable_!sequence!=%%A
    SET /A position+=1
    )

    FOR /F “tokens=1* delims==” %%A IN (‘SET some_variable_’) DO (
    ECHO “%%A” = “%%B”
    )

    EXIT /B

    • 0