user3289835
user3289835

Reputation: 25

Using variables to access windows command file parameters

I'm attempting to access a parameter passed to a function using a variable name. I know of other ways to do this, such as using the SHIFT command, but in this case I am trying it this way. I thought by using the "setlocal enabledelayedexpansion" command I could do this because it permitted the variable to be accessed using exclamation points around it rather than percent signs. This is the line from the script below that isn't working:

    set parm_value=%!parm_nr!

I thought that this would result in something like this:

    set parm_value=%1

but instead, it is resulting in:

    set parm_value=1

Does anyone know if this type of syntax is possible? Below is a test script demonstrating the issue. Everything except this one item is working correctly. Thank you in advance.

    :BOJ
        echo off
        setlocal enabledelayedexpansion
        cls
        echo.

        call :EXAMPLE_1 parm_1.1 parm_1.2 parm_1.3

        goto :EOJ

    :EXAMPLE_1
        set nr_parms=0
        for %%x in (%*) do set /A nr_parms+=1

        set parm_nr=1

    :EXAMPLE_1_LOOP
        if %parm_nr% GTR %nr_parms% goto :EXAMPLE_1_END

        set parm_value=%!parm_nr!
        echo Parameter #%parm_nr% : %parm_value%
        set /A parm_nr+=1
        goto :EXAMPLE_1_LOOP

    :EXAMPLE_1_END
        echo.
        exit /B 0

    :EOJ
        pause

Upvotes: 1

Views: 68

Answers (1)

npocmaka
npocmaka

Reputation: 57302

try with

call set parm_value=%%!parm_nr!

Upvotes: 1

Related Questions