0

My goal is to have a batch script that whenever launched if the machine is connected to my home wifi (SSID = The Sanctum Sanctorum - 5GHz) will launch Kodi from one folder ("C:\Program Files (x86)\Kodi at Home") but otherwise will launch Kodi from another ("C:\Program Files (x86)\Kodi").

Any help is much appreciated and thanks for your time I have attached updates with my code progress!

Edit #3 - Thanks to @treintje

Code Attempt #3

@echo off

set "ssid=The Sanctum Sanctorum - 5GHz"
netsh wlan show interfaces | findstr /r /c:"^ *SSID *: %ssid%$" 1>nul

if %errorlevel% equ 0 (
  set "kodi=Kodi at Home\Kodi.exe" "-p"
) else (
  set "kodi=Kodi\Kodi.exe"
)
"%programfiles(x86)%\%kodi%"

Now it seems even when connected to the specified network it always launches the "kodi=Kodi\Kodi.exe" version. I bet it's just a tiny syntax error I don't know to see as I'm sure treintje is right this is the way to do it!

Dracrius
  • 7
  • 1
  • 1
  • 7
  • In python https://stackoverflow.com/questions/33227160/how-do-i-get-python-to-know-what-wifi-the-user-is-connected-to – Teo Aug 11 '17 at 18:31
  • Firstly I've never used python so that'd be a nightmare learning a new language for one script seeing as that code barely touches the functions I want from it. Secondly I really don't think its even a valid suggestion seeing as I'm clearly asking for a batch script. It's what I know how to use write and modify plus it requires nothing more then a text editor! I'm not looking to setup a new compiler and coding environment. – Dracrius Aug 11 '17 at 18:58
  • @Dracrius If you want to execute the program (`"%programfiles(x86)%\%kodi%"`) and pass a parameter to it if the `findstr` command finds a match, you could simply set variable `kodi` like this: `set "kodi=Kodi at Home\Kodi.exe" "-p"` – 303 Aug 13 '17 at 20:25

1 Answers1

0

Assuming your machine only has a single wireless network interface card installed, you could try something like this:

@echo off

set "ssid=The Sanctum Sanctorum"
netsh wlan show interfaces | findstr /r /c:"^ *SSID *: %ssid%$" 1>nul

if %errorlevel% equ 0 (
  set "kodi=Kodi at Home\Kodi.exe" "-p"
) else (
  set "kodi=Kodi\Kodi.exe"
)

"%programfiles(x86)%\%kodi%"

The netsh wlan show interfaces command lists information about all wireless network interfaces including the SSID of any wireless network that they might be connected with. If the findstr command finds the SSID specified by the ssid variable, the errorlevel is set to zero.


If the target application cannot handle command-line switches that are encapsulated within quotes, you could set the kodi variable and call the application as such:

set "kodi=Kodi at Home\Kodi.exe" -p"

"%programfiles(x86)%\%kodi%
303
  • 2,417
  • 1
  • 11
  • 25
  • I think everything's working except I can't find a way to add the -p to the code to tell Kodi to run in portable mode. Otherwise both instances look exactly the same as they load from the AppData folder for Kodi. Preference would be to have only Kodi at Home run in portable mode but I can move the AppData info into the Local version of Kodi if its better. I'll post my attempts to my question. – Dracrius Aug 12 '17 at 23:07
  • Aiming it at a shortcut instead I can circumvent me not knowing how to append a launch parameter properly but for some reason the code appears to be reporting no errorlevel even if I disable my wifi adapter. – Dracrius Aug 13 '17 at 14:22
  • @Dracrius, I had forgotten to add the `/c` command-line switch to the `findstr` command which is required to ensure that the string argument is not treated as a space-separated list of search strings, even when regular expressions are used. I've updated my answer accordingly. – 303 Aug 13 '17 at 21:21
  • Thanks @treintje I'm really starting to understand whats going on and Im happy I was on the right track of how I should change it to add a parameter. Now I'm getting the revers issue though even when connected to my home wifi it's now launching the "kodi=Kodi\Kodi.exe" I tried the netsh wlan show interfaces in the CMD and can clearly see the SSID matchs so I'm stumped why the findstr isn't picking it up now. – Dracrius Aug 13 '17 at 23:01