Preface

I constantly switch between two wireless networks so I wrote a script that quickly switches between them. It only uses basic batch file commands that should be compatible with all verions of Windows but only tested with Windows 7.

Prerequisites

Notifu

Notifu is used to inform user of important information via the task tray. It is freely available from Parlint’s website.

Wireless network not found notification

Connecting to defined wireless network

Bat to EXE Converter

Converting the batch file into an executable can be done with Bat to EXE Converter. This step is only required to create an EXE for use with the Inno Setup installer. Simply launching the batch file would accomplish the same task but creating an installer makes it easy to share with others and to include other required software like Notifu. The Bat to EXE Converter is availble from f2ko.de’s website.

Inno Setup

Inno Setup is used to create an installer for the executable created with Bat to EXE Converter. This enables the user to simply double click an icon to switch between the two networks. Or optional pin to the taskbar for rapid switching. It is freely available from jrsoftware’s website.

The Batch Code

The built in Windows function netsh wlan is the primary function used to discover the wireless networks. Notifu.exe must be in the same directory has this batch file to work properly. View the comments in the code snippet below for a description of each code block.

@echo off

REM Setup networks to toggle between
set "ssid1=<insert first SSID name here>"
set "ssid2=<insert second SSID name here>"

REM Search for currently connected network
REM If no network is connected, default is ssid1
netsh wlan show interfaces | findstr /r /c:"^ *SSID *: %ssid1%$" 1>nul

if %errorlevel% equ 0 (
	REM Connection was found to ssid1, toggle to ssid2
	echo Connected to SSID '%ssid1%'.
	set "ssid=%ssid2%"
) else if %errorlevel% equ 1 (
	REM Connection was found to ssid2, toggle to ssid1
	echo Disconnected or connected to SSID '%ssid2%'.
	set "ssid=%ssid1%"
) else (
	REM findstr error code 2
	echo findstr failed, double check batch file code
	goto end
)

REM Make sure new ssid is available before attempting connection
netsh wlan show networks | findstr /r /c:"^SSID [0-9]* *: %ssid%$" 1>nul
if %errorlevel% equ 0 (
	REM do nothing
) else (
	start notifu /m "SSID '%ssid%' was not found!" /p "Toggle Networks"
	goto end
)

REM Make sure profile is available for switching
netsh wlan connect name="%ssid%"
if %errorlevel% equ 0 (
	start notifu /m "Switching to SSID '%ssid%'." /p "Toggle Networks"
) else (
	start notifu /m "No profile available for '%ssid%' SSID!" /p "Toggle Networks" 
	goto end
)

:end
REM Debugging pause
REM pause

Batch File to EXE Conversion

Using Bat To EXE Converter the batch file can easily be converted via the command line to an EXE file. Place the Bat_To_Exe_Converter.exe in the same directory as toggle_networks.bat and notifu.exe. Optionally include an ICO icon file for the EXE file.

Bat_To_Exe_Converter.exe /bat toggle_networks.bat /exe toggle_networks.exe /include notifu.exe /icon toggle_networks.ico /invisible

Using Inno Setup to Create Installer

Open Inno Setup, File > New, and walk through the setup wizard dialogs. An ISS setup script will be generated like below.

; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

#define MyAppName "Toggle Networks"
#define MyAppVersion "1.0.3"
#define MyAppPublisher "Aric Beaver"
#define MyAppURL "http://aricbeaver.com/"
#define MyAppExeName "toggle_networks.exe"

[Setup]
; NOTE: The value of AppId uniquely identifies this application.
; Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={ {F54DD77C-3DE2-480C-944B-D2EAD7C74653}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={pf}\{#MyAppName}
DisableProgramGroupPage=yes
OutputBaseFilename=setup
Compression=lzma
SolidCompression=yes

[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"

[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}";

[Files]
Source: "C:\Users\fg376t\Documents\network\toggle_networks.exe"; DestDir: "{app}"; Flags: ignoreversion
; NOTE: Don't use "Flags: ignoreversion" on any shared system files

[Icons]
Name: "{commonprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{commondesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon

Once the script has been generated, there is no need to open Inno Setup anymore for future updated installers. Simply run Inno Setup via the command line like this.

"C:\Program Files (x86)\Inno Setup 5\Compil32.exe" /cc toggle_networks.iss

Download

All of the source code and required prerequisites can be downloaded from the link below.

Toggle Networks - Version 1.0.3