我有此代码:
@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%]%%
在内部,我尝试使用
将从数组中获取的值分配给变量设置interfejs = %% NetInterfNames [%_ inputname%] %%
问题是未分配值形式数组。 有没有人知道如何处理?
答案 0 :(得分:1)
这里并没有启用延迟扩展,而是根据我的评论建议,向您显示您需要使用额外的Call
。 (我添加了Pause
,让您可以看到任何输出):
@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%]%%
但是,如果您移动两个If
命令,则完全可以不需要其中一个代码块:
@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%]%%
您可能还可以在不启用延迟扩展的情况下进行一些改进:
@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
或扩展延迟:
@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
答案 1 :(得分:0)
如果您enabledelayedexpansion
,它将帮助您轻松实现这一目标。
@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%]!