Search code examples
batch-fileif-statementgoto

Using batch to controll my network adapters


I'm trying to write a simple batch file that will ask me if i want to enable or disable my network adapter, and then pass my answer on to netsh. (I like to turn it off when watching Netflix on my TV, seems to help quality)

I've written this:

set /p %letter%=[(E)nable or (D)isable:]
IF %letter=="e". goto enable
IF %letter=="d". goto disable

:enable
set command=enable
netsh interface set interface name="Local Area Connection" admin=%command%
Pause
exit

:disable
set command=disable
netsh interface set interface name="Local Area Connection" admin=%command%
pause
exit

But the "goto's never seem to get followed. it always picks the first subroutine.


Solution

  • Remove the % around the variable in the set command too:

    :again
    set /p letter=[(E)nable or (D)isable:]
    IF /i "%letter%"=="e" goto enable 
    IF /i "%letter%"=="d" goto disable
    echo D or E please!
    goto again
    

    The strings in each side of the == must exactly match. The /i switch makes the comparison case-insensitive. If neither matches, then the next line is executed - hence always going to enable