Batch file 使用批处理文件将本地IP存储在环境变量中

Batch file 使用批处理文件将本地IP存储在环境变量中,batch-file,ip,environment-variables,Batch File,Ip,Environment Variables,我试图将我的本地ip存储到一个系统变量中,但我正在努力处理该批处理文件。我已经能够解析我的IP并提取正确的子字符串。然而,声明变量并将其传递给SETX似乎比我想象的要困难 任何帮助都将不胜感激。:) @echo关闭 延迟扩展 ::这里只是一个示例适配器: 设置“适配器=以太网适配器VirtualBox主机专用网络” 设置adapterfound=false 回声网络连接测试 对于(`ipconfig/all`)中的/f“usebackq令牌=1-2 delims=:”%%f( 设置“项=%%f”

我试图将我的本地ip存储到一个系统变量中,但我正在努力处理该批处理文件。我已经能够解析我的IP并提取正确的子字符串。然而,声明变量并将其传递给SETX似乎比我想象的要困难

任何帮助都将不胜感激。:)

@echo关闭
延迟扩展
::这里只是一个示例适配器:
设置“适配器=以太网适配器VirtualBox主机专用网络”
设置adapterfound=false
回声网络连接测试
对于(`ipconfig/all`)中的/f“usebackq令牌=1-2 delims=:”%%f(
设置“项=%%f”
如果/i“!item!”==“!适配器!”(
设置adapterfound=true
)else如果不是“!item!”==”!item:IPv4地址=!“如果”!adapterfound!”==“true”(
echo%%g | cut-d”(“-f1 | tr-d”[:space:]”改进的脚本(可能与区域设置有关):

另一种方法:

@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
:: just a sample adapter here:
set "adapter=Ethernet-Adapter VirtualBox Host-Only Network"
echo Network Connection Test
:: get adapter index
set "_where=where "NetConnectionID = '%adapter%'""
for /F "tokens=1,* delims==" %%G in ('
wmic path Win32_NetworkAdapter %_where% get InterfaceIndex /value ^| findstr "="
') do for /F %%g in ("%%H") do set "_ii=%%~g" 
:: get adapter's IP addresses
set "_where=where "InterfaceIndex = '%_ii%'""
for /F "tokens=1,* delims==" %%G in ('
wmic path Win32_NetworkAdapterConfiguration %_where% get IPAddress /value ^| findstr "="
') do for /F "tokens=1,2 delims={,}" %%g in ("%%H") do (
        set "_IPv4=%%~g" 
        set "_IPv6=%%~h"
      )
set _ip
此处显示了
循环的

  • %%G
    检索所需值
  • %%g
    删除返回值中的结束回车:
    wmic
    行为:每个输出行以
    0x0D0D0A
    )结束,而不是普通的
    0x0D0A

查看Dave Benham的

您知道启用延迟扩展
。然后,使用
!ipadress!
而不是
%ipadress%
!ipadress!给我一个语法错误您需要与此特定适配器关联的确切ipv4地址还是只需要首选的本地ip地址?不幸的是,我需要确切的适配器。但这是wor我只想把“%%g | cut-d”(“-f1 | tr-d”[:space:]”的字符串结果塞进一个变量,然后将其传递给setx命令。
@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
::just a sample adapter here:
set "adapter=Ethernet-Adapter VirtualBox Host-Only Network"
set "adapterfound=false"
echo Network Connection Test
for /f "usebackq tokens=1-2 delims=:" %%f in (`ipconfig /all`) do (
    set "item=%%f"
    if not "!item!"=="!item:%adapter%=!" (
        set "adapterfound=true"
        echo %adapter%
    ) else if not "!item!"=="!item:IPv4 Address=!" if "!adapterfound!"=="true" (
           rem                     ↑↑↑↑↑↑↑↑↑↑↑↑ might be locale-dependent
        for /F "delims=( " %%G in ("%%g") do set "ipadress=%%G"
        echo !ipadress!
        rem setx MY_IP !ipadress!
        set "adapterfound=false"
    )
)
echo %ipadress%
@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
:: just a sample adapter here:
set "adapter=Ethernet-Adapter VirtualBox Host-Only Network"
echo Network Connection Test
:: get adapter index
set "_where=where "NetConnectionID = '%adapter%'""
for /F "tokens=1,* delims==" %%G in ('
wmic path Win32_NetworkAdapter %_where% get InterfaceIndex /value ^| findstr "="
') do for /F %%g in ("%%H") do set "_ii=%%~g" 
:: get adapter's IP addresses
set "_where=where "InterfaceIndex = '%_ii%'""
for /F "tokens=1,* delims==" %%G in ('
wmic path Win32_NetworkAdapterConfiguration %_where% get IPAddress /value ^| findstr "="
') do for /F "tokens=1,2 delims={,}" %%g in ("%%H") do (
        set "_IPv4=%%~g" 
        set "_IPv6=%%~h"
      )
set _ip