1

I have this code:

@echo off
SET /a counter=0
SET _inputname=
Set interfejs=ala
FOR /f "skip=3 tokens=*" %%a in ('netsh int ip show interfaces') do ( call :myfunct "%%a" )

SET /P _inputname=interface: 
IF "%_inputname%" LEQ "%counter%" IF "%_inputname%" GTR "0" (
echo variable value:%_inputname%
call echo [%_inputname%] %%NetInterfNames[%_inputname%]%%

rem problem with assigning array value to a variable.

Set interfejs=%%NetInterfNames[%_inputname%]%%
echo to jest wybrany interface: %interfejs%

Rem split string
rem for /F "tokens=1,*" %%a in ("hello how are you") do echo %%b

)   
IF "%_inputname%" GTR "%counter%" ( GOTO :EOF )
IF "%_inputname%" EQU "0" ( GOTO :EOF )

    goto: EOF

:myfunct
set /a counter=%counter%+1
set NetInterfNames[%counter%]=%~1
call echo [%counter%] %%NetInterfNames[%counter%]%%

Inside I'm trying to assign a value taken from array to variable using this

Set interfejs=%%NetInterfNames[%_inputname%]%%

Problem is that value form array is not assigned. Does anyone have an idea of what to do with it?

6
  • Firstly. When using leq gtr etc. it is better to not enclose the variables in double quotes. i.e if %var% leq 10 … Secondly, the correct method to do addition is set /a counter+=1 Lastly, What is the exact expected output? It is difficult to guess what you require to be the outcome. Commented Oct 11, 2019 at 11:19
  • As you're trying to do this without enabling and using delayed expansion, you should be invoking the expansion via a call when setting interferjs, i.e. Call Set "interfejs=%%NetInterfNames[%_inputname%]%%". You've used call only when echoing, but seemingly forgotten, to do so to set the variable value itself. Commented Oct 11, 2019 at 11:38
  • This code returns a list of all network interfaces in windows. So after choosing one, I would like to assign it to a variable so then I could use it later Commented Oct 11, 2019 at 11:50
  • I test it Call Set "interfejs=%%NetInterfNames[%_inputname%]%%" and it doesnt work. Commented Oct 11, 2019 at 11:55
  • 1
    Just use enabledelayedexpansion instead Commented Oct 11, 2019 at 12:00

2 Answers 2

1

And here it is without enabling delayed expansion, to show you that you needed to use an additional Call, as per my commented advice. (I've added a Pause to allow you to see any output):

@Echo Off
Set "counter=0"
Set "_inputname="
Set "interfejs=ala"
For /F "Skip=3 Tokens=*" %%A In ('NetSh int ip show interfaces') Do Call :myfunct "%%A"

Set /P "_inputname=interface: "
If "%_inputname%" LEq "%counter%" If "%_inputname%" Gtr "0" (
    Echo variable value:%_inputname%
    Call Echo [%_inputname%] %%NetInterfNames[%_inputname%]%%

    Rem no problem with assigning array value to a variable.

    Call Set "interfejs=%%NetInterfNames[%_inputname%]%%"
    Call Echo to jest wybrany interface: %%interfejs%%

    Rem split string
    Rem For /F "Tokens=1*" %%A In ("hello how are you") Do Echo %%B

)
If "%_inputname%" Gtr "%counter%" GoTo :EOF
If "%_inputname%" Equ "0" GoTo :EOF

Pause&GoTo :EOF

:myfunct
Set /A counter+=1
Set "NetInterfNames[%counter%]=%~1"
Call Echo [%counter%] %%NetInterfNames[%counter%]%%

However if you move two of your If commands, you can completely negate the need for one of the code blocks:

@Echo Off
Set "interfejs=ala"

Set "counter=0"
For /F "Skip=3 Tokens=*" %%A In ('NetSh int ip show interfaces') Do Call :myfunct "%%A"

Set "_inputname="
Set /P "_inputname=interface: "
If Not Defined _inputname GoTo :EOF
If "%_inputname%" Gtr "%counter%" GoTo :EOF
If "%_inputname%" Equ "0" GoTo :EOF
Echo variable value:%_inputname%
Call Echo [%_inputname%] %%NetInterfNames[%_inputname%]%%
Call Set "interfejs=%%NetInterfNames[%_inputname%]%%"
Echo to jest wybrany interface: %interfejs%

Rem split string
Rem For /F "Tokens=1*" %%A In ("hello how are you") Do Echo %%B

Pause&GoTo :EOF

:myfunct
Set /A counter+=1
Set "NetInterfNames[%counter%]=%~1"
Call Echo [%counter%] %%NetInterfNames[%counter%]%%

You could probably improve things a little too, still without enabling delayed expansion:

@Echo Off
For /F "Delims==" %%A In ('Set NetInterfNames[ 2^>NUL')Do Set "%%A="
For /F "Tokens=1* Delims=:" %%A In (
    'NetSh int ip show interfaces 2^>NUL^|Findstr "[0-9]"^|FindStr /N "^"'
)Do Set "NetInterfNames[%%A]=%%B"&Call Echo [%%A] %%NetInterfNames[%%A]%%
:Input
Set /P "_inputname=interface: "
If Not Defined NetInterfNames[%_inputname%] GoTo Input
Call Set "_interfejs=%%NetInterfNames[%_inputname%]%%"
Echo to jest wybrany interface: %_interfejs%

Rem split string
Rem For /F "Tokens=1*" %%A In ("hello how are you") Do Echo %%B

Pause&GoTo :EOF

Or with delayed expansion:

@Echo Off
SetLocal EnableDelayedExpansion
For /F "Delims==" %%A In ('Set NetInterfNames[ 2^>NUL')Do Set "%%A="
For /F "Tokens=1* Delims=:" %%A In (
    'NetSh int ip show interfaces 2^>NUL^|Findstr "[0-9]"^|FindStr /N "^"'
)Do Set "NetInterfNames[%%A]=%%B"&Echo [%%A] !NetInterfNames[%%A]!
:Input
Set /P "_inputname=interface: "
If Not Defined NetInterfNames[%_inputname%] GoTo Input
Set "_interfejs=!NetInterfNames[%_inputname%]!"
Echo to jest wybrany interface: %_interfejs%

Rem split string
Rem For /F "Tokens=1*" %%A In ("hello how are you") Do Echo %%B

Pause&GoTo :EOF
Sign up to request clarification or add additional context in comments.

Comments

0

If you enabledelayedexpansion it will help you achieve this much easier.

@echo off
setlocal enabledelayedexpansion
SET /a counter=0
SET _inputname=
Set interfejs=ala
FOR /f "skip=3 tokens=*" %%a in ('netsh int ip show interfaces') do call :myfunct "%%~a"

SET /P _inputname=interface: 
IF %_inputname% LEQ %counter% IF %_inputname% GTR 0 (
echo variable value:%_inputname%
echo [%_inputname%] !NetInterfNames[%_inputname%]!

Set interfejs=!NetInterfNames[%_inputname%]!
echo to jest wybrany interface: !interfejs!

)   
IF %_inputname% GTR %counter% GOTO :EOF
IF %_inputname% EQU 0 GOTO :EOF

    goto :eof

:myfunct
set /a counter+1
set NetInterfNames[%counter%]=%~1
call echo [%counter%] !NetInterfNames[%counter%]!

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.