Question

I have this batch which works to set a staic ip and subnet and back to dhcp, which works perfectly in Vista. However when trying to put it into a colleagues XP machine i get an issue with the IF Exist Rename.

Vista Code:

@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 Code: (had to change a few things to get the netsh command to work)

@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)

The problem is the :end piece where i try to rename the file as a sort of toggle switch. this will not work in xp for some reason.(it also renames the file if it is named wrong)

IF EXIST %~dp0static.bat (ren %~dp0static.bat dhcp.bat) else (ren %0 static.bat)

Its probably something simple there's probably a better way to do this that will work in all versions of windows, any help would be greatly appreciated. Thanks in advance.

Was it helpful?

Solution

Instead of this (ren %0 static.bat) use this (ren "%~f0" static.bat)

That should fix the rename issue where the batch file is launched by typing in the name.

Choice is not native to XP either... you can use ping -n 4 localhost >nul

OTHER TIPS

fixed quotes required around firest file location IF EXIST "%~dp0one.bat" (ren "%~f0" two.bat) else (ren "%~f0" one.bat)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top