批处理修改IP地址。原IP最后一位保留。只修改前三位。为什么出错?显示b不可接受值

2025-03-23 11:40:28
推荐回答(1个)
回答1:

@echo off
echo 正在更改IP地址,请稍等......
for /f "tokens=2 delims=:" %%a in ('ipconfig ^| find /i "ip address"') do (
    for /f "tokens=4 delims=." %%b in ("%%a") do (
        echo %%b
        netsh interface ip set address name="本地连接" source=static addr=10.40.164.%%b mask=255.255.255.0 gateway=10.40.17.254 gwmetric=1
    )
)
netsh interface ip set dns name="本地连接" source=static addr=202.96.128.86
netsh interface ip add dns name="本地连接" addr=202.96.128.166 index=2
ipconfig /flushdns
ipconfig /all
echo 更改IP地址完成!
goto end

:end
pause

在批处理里,for中的%%i 这类变量只能在for中调用,除了for是不能使用的,如果要出for使用,就需要先在for中进行set变量设置,然后在for外调用set的变量。

所以您代码不是说用%^b就能在for外调用的,而且也没有这样的用法。因此语法编写错误,导致代码执行失败。