我有这个批,它可以设置一个静态ip和子网,然后返回到dhcp,这在Vista中非常有效。然而,当试图将其放入同事XP机器时,我遇到了IF Exist-Rename的问题。
Vista代码:
@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)
问题是:我试图将文件重命名为一种切换开关的结尾部分。由于某些原因,这在xp中不起作用。(如果命名错误,它还会重命名文件)
IF EXIST %~dp0static.bat (ren %~dp0static.bat dhcp.bat) else (ren %0 static.bat)
这可能很简单——可能有一种更好的方法可以在所有版本的windows中工作,任何帮助都将不胜感激。提前谢谢。
使用此(ren "%~f0" static.bat)
而不是此(ren %0 static.bat)
这应该可以解决通过键入名称来启动批处理文件的重命名问题。
Choice也不是XP自带的。。。您可以使用ping -n 4 localhost >nul
第一个文件位置IF EXIST "%~dp0one.bat" (ren "%~f0" two.bat) else (ren "%~f0" one.bat)
周围需要的固定引号