0

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.

Chumbawamba
  • 121
  • 2
  • 11

2 Answers2

3

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

foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • Thanks ill give that a try when i get home what about `(ren %~dp0static.bat dhcp.bat)` would that need to be `(ren "%~f0" dhcp.bat)`? and would `IF EXIST %~dp0static.bat` work fine because i could not seem to see which part of that command did and didn't work. If your using XP could you try `IF EXIST %~dp0static.bat (ren "%~f0" dhcp.bat) else (ren "%~f0" static.bat)` – Chumbawamba Jul 04 '13 at 12:52
  • your code here should work on any NT version of windows. `IF EXIST %~dp0static.bat (ren "%~f0" dhcp.bat) else (ren "%~f0" static.bat)` The issue was probably the way it was being launched, as the same issue would affect Win7 and Win8 etc in that way too. %0 is what is typed at the command line, or it becomes the full drv:\path\filename.bat when it's clicked on, or drag and dropped. – foxidrive Jul 04 '13 at 13:16
  • Ive made those changes and it all seems to work fine in Vista will try on XP when i get home Thanks for your Help – Chumbawamba Jul 04 '13 at 15:08
  • Tried this in xp and it does not work `IF EXIST %~dp0one.bat (ren "%~f0" two.bat) else (ren "%~f0" one.bat) pause` – Chumbawamba Jul 05 '13 at 18:09
  • `ren %0 one.bat` works fine in xp it seems to be the if exist thats not working – Chumbawamba Jul 05 '13 at 18:17
1

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

Chumbawamba
  • 121
  • 2
  • 11