Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

batch file

Options
  • 18-02-2010 11:36am
    #1
    Registered Users 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 Posts: 21,432 ✭✭✭✭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: 90,852 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 Posts: 40 chapod21


    cheers for the advice


Advertisement