我有这批可以设置静态 ip 和子网并返回 dhcp,它在 Vista 中完美运行。但是,当尝试将其放入同事 XP 机器时,我遇到了 IF Exist Rename 的问题。
远景代码:
@echo off
cls
:start
if "%~n0"=="static" goto static
if "%~n0"=="dhcp" goto dhcp
echo Batch file MUST be named (static or dhcp)
echo File will be renamed static.bat
pause
goto end
:static
set /p craig1= IP Address?
set /p craig2= Subnet Mask?
echo Setting IP to %craig1% and Subnet mask to %craig2%
netsh int ip set address "local area connection" static %craig1% %craig2%
echo Waiting for IP to update...
@choice /c 12 /T 3 /d 1 > NUL
ipconfig /all
cls
echo Successfully Set A Static IP!
pause
goto end
:dhcp
echo Setting Dynamic (DHCP) IP
netsh int ipv4 set address "Local Area Connection" dhcp
echo Setting Dynamic (DHCP) DNS
netsh int ipv4 set dnsserver "Local Area Connection" dhcp
echo Waiting for IP to update...
@choice /c 12 /T 3 /d 1 > NUL
ipconfig /all
cls
echo Successfully Set To Obtain IP By DHCP!
pause
goto end
:end
IF EXIST %~dp0static.bat (ren %~dp0static.bat dhcp.bat) else (ren %0 static.bat)
XP 代码:(必须更改一些内容才能使 netsh 命令正常工作)
@echo off
cls
:start
if "%~n0"=="static" goto static
if "%~n0"=="dhcp" goto dhcp
echo Batch file MUST be named (static or dhcp)
echo File will be renamed static.bat
pause
goto end
:static
set /p craig1= IP Address?
set /p craig2= Subnet Mask?
echo Setting IP to %craig1% and Subnet mask to %craig2%
netsh int ip set address "local area connection" static %craig1% %craig2%
echo Waiting for IP to update...
@choice /c 12 /T 3 /d 1 > NUL
ipconfig /all
cls
echo Successfully Set A Static IP!
pause
goto end
:dhcp
echo Setting Dynamic (DHCP) IP
netsh int **ip** set address "Local Area Connection" dhcp
echo Setting Dynamic (DHCP) DNS
netsh int **ip** set **dns** "Local Area Connection" dhcp
echo Waiting for IP to update...
@choice /c 12 /T 3 /d 1 > NUL
ipconfig /all
cls
echo Successfully Set To Obtain IP By DHCP!
pause
goto end
:end
IF EXIST %~dp0static.bat (ren %~dp0static.bat dhcp.bat) else (ren %0 static.bat)
问题是 :end 部分,我尝试将文件重命名为一种拨动开关。由于某种原因,这在 xp 中不起作用。(如果文件命名错误,它也会重命名文件)
IF EXIST %~dp0static.bat (ren %~dp0static.bat dhcp.bat) else (ren %0 static.bat)
它可能很简单,可能有更好的方法可以在所有版本的 Windows 中工作,任何帮助将不胜感激。提前致谢。