Combining batch files so double click performs one operation and the next double click reverses or performs the next operation?

So I have two separate batch files. One to disable the NIC and enable the wifi card:

@netsh interface set interface name="Ethernet" admin=disabled
@netsh interface set interface name="Wi-Fi" admin=Enabled

and another to do the opposite (enable the NIC and disable the wifi card):

@netsh interface set interface name="Wi-Fi" admin=disabled
@netsh interface set interface name="Ethernet" admin=Enabled

How do I combine them into one batch that I can double click to toggle one way and double click again to toggle the other way?


Solution 1:

enter image description here


I've already answered a similar question, and by looking for something "better" than my own answer, I've offered an bounty at that time.

What came to deserve it was a @UnhandledExcepSean answer, it comes down to using a value in the register, where in your case, reading and writing to then "perceive" the relevant on/off actions...

@echo off & setlocal

pushd %__AppDir__% && 2>nul =;(
    reg.exe query HKCU\Environment /d /e /f on|find.exe /i "Click" >nul && =;(
    netsh.exe interface set interface name="Ethernet" admin=Disabled
    netsh.exe interface set interface name="Wi-Fi" admin=Enabled 
    setx.exe Click off >nul & endlocal & goto :eof                  );= || =;( 
    netsh.exe interface set interface name="Ethernet" admin=Enabled
    netsh.exe interface set interface name="Wi-Fi" admin=Disabled 
    setx.exe Click on 1>nul & endlocal & goto :eof                  );= );=

  • One alternative:
@echo off

setlocal & pushd %__AppDir__% && 2>nul =;(
    reg.exe query HKCU\Environment /d /e /f on|find.exe /i "Click" >nul && =;(
    set "_Eth=Disable" && set "_Wi-Fi=Enabled" && set "_click=off" ) || =;(
    set "_Eth=Enabled" && set "_Wi-Fi=Disable" && set "_click=on"  ) );=    

netsh.exe interface set interface name="Ethernet" admin=%_Eth%
netsh.exe interface set interface name="Wi-Fi" admin=%_Wi-Fi%
setx.exe Click %_Click% 1>nul & popd & endlocal & exit /b

Solution 2:

How do I combine them into one batch file?

netsh interface show interface shows Admin State as Enabled or Disabled for the interfaces.

The following batch file parses the output of the above command and toggles the state of the two connections.

@echo off
setlocal enabledelayedexpansion
rem run netsh and get admin status and interface name
rem skip headers
for /f "tokens=1,4* skip=3" %%f in ('netsh interface show interface') do (
  set _admin=%%f
  set _interface=%%g %%h
  if "!_interface!" == "Wi-Fi" (
    if "!_admin!" == "Enabled" (
      netsh interface set interface name="!_interface!" admin=disabled
      ) else (
      netsh interface set interface name="!_interface!" admin=enabled
      )
    )
  if "!_interface!" == "Ethernet" (
    echo !_interface!
    if "!_admin!" == "Enabled" (
      netsh interface set interface name="!_interface!" admin=disabled
      ) else (
      netsh interface set interface name="!_interface!" admin=enabled
      )
    )
  )
endlocal

Further Reading

  • An A-Z Index of the Windows CMD command line | SS64.com
  • Windows CMD Commands (categorized) - Windows CMD - SS64.com
  • EnableDelayedExpansion - Windows CMD - SS64.com
  • For /f - Loop through command output - Windows CMD - SS64.com
  • Netsh - Windows CMD - SS64.com
  • Netsh commands for Interface IP
  • Netsh Commands for Wireless Local Area Network (WLAN)