如果存在则转换从vista重命名为xp

时间:2013-07-04 10:52:10

标签: batch-file cmd netsh

我有这个批处理可以设置一个staic 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中使用,任何帮助都将非常感激。提前谢谢。

2 个答案:

答案 0 :(得分:3)

而不是(ren %0 static.bat)使用此(ren "%~f0" static.bat)

这应该通过输入名称来解决重命名问题,即重命名问题。

选择不是XP本机的......您可以使用ping -n 4 localhost >nul

答案 1 :(得分:1)

在firest文件位置IF EXIST "%~dp0one.bat" (ren "%~f0" two.bat) else (ren "%~f0" one.bat)

周围需要修复引号