Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 在特定接口的netsh中循环并将IP地址存储到变量_Batch File - Fatal编程技术网

Batch file 在特定接口的netsh中循环并将IP地址存储到变量

Batch file 在特定接口的netsh中循环并将IP地址存储到变量,batch-file,Batch File,我试图将接口环回的IP地址分配给一个变量,但由于某些原因,我什么也得不到。 如果我在命令shell中键入该命令,则此操作有效: C:\temp>netsh interface ip show config name="Loopback" | find "IP Address" IP Address: 192.168.255.10 但当我循环它时,输出是空的 set _netsh_cmd=netsh interface ip show

我试图将接口环回的IP地址分配给一个变量,但由于某些原因,我什么也得不到。 如果我在命令shell中键入该命令,则此操作有效:

C:\temp>netsh interface ip show config name="Loopback" | find  "IP Address"
IP Address:                           192.168.255.10
但当我循环它时,输出是空的

set _netsh_cmd=netsh interface ip show config name="Loopback"
FOR /f "tokens=*" %%G IN ('%_netsh_cmd% ^| find "IP Address"') DO echo [%%G]

如果发现错误,您必须启用delayedExpansion,并将
\u netsh\u cmd
包围起来而不是
%
。这应该是正确的:

setlocal enableDelayedExpansion
set _netsh_cmd=netsh interface ip show config name="Loopback"
FOR /f "tokens=*" %%G IN ('!_netsh_cmd! ^| find "IP Address:"') DO echo [%%G]

工作正常,没有延迟检查

@echo off
set _netsh_cmd=netsh interface ip show config "Loopback"
FOR /f "delims=" %%G IN ('%_netsh_cmd% ^|find "IP Address"') DO echo [%%G]

我不确定这是否是一个错误,但在for循环中,您在find字符串中添加了冒号。谢谢,但我已经尝试了使用和不使用,没有问题。我通常通过始终启用
delayedExpansion
来避免代码块中的大多数错误,并且很少使用
%
,即使这没有什么区别。