Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

batch file

  • 18-02-2010 11:36AM
    #1
    Registered Users, Registered Users 2 Posts: 40


    Hi

    I am writing a batch file to change from static to dynamic ip addresses

    My first attempt works fine and you can see the code below

    @echo off
    ECHO Enter 1 for static or 2 for DCHP and press enter when finished
    ECHO ...
    set /p var1= value =
    IF %var1%==1 (netsh interface ip set address name="Local Area Connection" static 12.12.12.20 255.255.255.0 12.12.12.1 1) ELSE (IF %var1%==2 (netsh interface ip set address "Local Area Connection" dhcp) ELSE ( ECHO Please run the program again and enter the value 1 or 2 Thanks ))


    The problem I have is when I want to add and option to manually set the static IP address. When the batch file is run the var2 var3 and var4 are set to empty. What I need to happen is the netsh command to use the new values that I will enter. The code is below

    ECHO Enter 1 for static , 2 for DCHP and press or 3 to change default address enter when finished

    ECHO ...
    set /p var1= value =
    IF %var1%==3 ( ECHO "Please enter IP address"
    set /p var2= value =
    ECHO Please enter Netmask
    set /p var3= value =
    ECHO PLease Enter Gateway
    set /p var4= value =
    netsh interface ip set address name="Local Area Connection" static %var2% %var3% %var4%)

    IF %var1%==1 (netsh interface ip set address name="Local Area Connection" static 12.12.12.20 255.255.255.0 12.12.12.1 1) ELSE (IF %var1%==2 (netsh interface ip set address "Local Area Connection" dhcp) ELSE ( ECHO Please run the program again and enter the value 1 or 2 Thanks ))
    sleep 1

    Thanks


Comments

  • Registered Users, Registered Users 2, Paid Member Posts: 21,529 ✭✭✭✭Alun


    Basically you can't set and use an environment variable inside a 'loop', i.e. anything with in parentheses ( ), unless you both enable delayed expansion (setlocal enabledelayedexpansion at start of batch file) and refer to any variables used inside that loop using exclmation marks instead of % signs, i.e. !var1! instead of %var1%.


  • Moderators, Recreation & Hobbies Moderators, Science, Health & Environment Moderators, Technology & Internet Moderators Posts: 96,027 Mod ✭✭✭✭Capt'n Midnight


    suggestions
    you may be better off using %1 etc. rather than asking the user

    also can have default values to save time

    IP.BAT 10.0.0.1 255.0.0.0 10.0.0.254
    IP.BAT 10.0.0.1 8 10.0.0.254

    If [%1]==[] display message

    If [%1]==[1] dhcp

    REM now use %1 as IP address

    REM use %2 as mask
    REM try to save user typing
    If [%2]==[] set %m=255.255.255.0
    If [%2]==[24] set %m=255.255.255.0
    If [%2]==[16] set %m=255.255.0.0
    If [%2]==[8] set %m=255.0.0.0
    if [%m]==[] set %m==%2



    split the long line with goto's so you can see what actually happens
    but watch out for http://xkcd.com/292/

    if []==[2] goto :DHCP
    echo variables and stuff
    netsh for DHCP
    goto :SLEEP1

    :DHCP
    echo stuff
    netsh for static IP - after script runs you can cut/paste from screen to debug

    :SLEEP1





    another way to get DHCP is to use netsh to reset the card


  • Registered Users, Registered Users 2 Posts: 40 chapod21


    cheers for the advice


Advertisement