TL;DR at the bottom!
Details here:
I have an unreliable internet connection due to 2 reasons on my Win 10 laptop, which is connected wirelessly to my router, which is connected by wire to my modem.
-
Reason 1, something that isn't Win 10's fault:
My modem often fritzes out (IDK if it's the modem itself, the ISP, or what, but yeah), whereby it kinda just restarts. When this happens, the internet on my laptop (obviously) goes out too, but it then doesn't reconnect when the modem comes back on. The only way it'll do so is if I disable, then enable, my wireless adapter from the Network Connections window. -
Reason 2, something that is Win 10's fault:
My laptop sometimes gets kicked off the Wi-Fi network. This itself might be caused by something other than Win 10, like my NIC, or my router. What annoys me is when Win 10 then doesn't automatically reconnect to the network even though it's supposed to. When I click on the "Connect" button in the Wi-Fi menu myself, it works fine.
I decided to make a *.bat file that would detect whenever either of the scenarios occurred, and then fix the problem. With a little bit of research on the internet, I managed to put together the following script:
@echo off
:LOOP
ping 8.8.8.8
IF ERRORLEVEL 1 goto RESTART
IF ERRORLEVEL 0 goto LOOP
:RESTART
netsh interface set interface "<wireless adapter>" disabled
netsh interface set interface "<wireless adapter>" enabled
netsh wlan connect name="<ssid>" interface="<wireless adapter>"
timeout /t 15
goto LOOP
This works great (when it's Run as Administrator), but now the problem is that I want the cmd window to not remain open (on screen, and in the taskbar) while the process runs; I want it to run in the tray instead, so I can still easily exit it if I need to, without having to go to the Task Manager. I know I can schedule the script in the Task Scheduler to start minimized on start-up, but won't that just hide the window from taskbar?
TL;DR:
How do I make a *.bat file run in the tray / by the clock where I can easily right-click it and stop/exit?
Is compiling an executable the only way to achieve this?
Best Answer
Since I don't believe this can be done through a simple
.bat
file, this seem like a job for AutoHotKey.According to this forum post from 2014 you should be able to toggle window visibility with a script similar to this:
This script was tested with AutoHotKey 1.1.24.00 (May 2016).
EDIT: Here is a link to the OPs modified version with improvements.
In this instance:
Simply save the script as something like
tray.ahk
(note the.ahk
script extension) and double click to run it (assuming you have installed AutoHotKey).The script starts the batch file minimized and replaces the default AHK script icon with a miniature console window icon in the tray.
Console window visibility can be toggled with a double click of the tray icon or the added tray menu item (appearing in bold at the bottom of the selection menu).
Closing both the batch file and the command window can be done with the added
Close
menu item belowShow / Hide
(Exit
will only close the script, not the console window).Also note that
will obviously need
pingu.bat
to be replaced with the name of your own batch file. This line assumes that the AHK script appears in the same directory aspingu.bat
or thatpingu.bat
is globally accessible (i.e. it has been added to a folder in your system Path or user PATH variables). Otherwise, you will want to replacepingu.bat
with the full path to the executable (watch for spaces!).Caveats
This script mostly acts as an interface to window visibility. What this means is that if you
Exit
via the tray icon, you are only exiting the AHK script, not your batch file.The simple solution is to use the added
Close
menu item to exit both the script and stopcmd.exe
. Alternatively, you can:Show the console window and use Ctrl + C (or simply close the console window with the red 'X') to terminate the batch process.
Select
Exit
from the tray icon to stop the AHK scriptThey are separate processes, as mentioned.
Note: Orphaned console programs with hidden windows cannot be directly accessed again if the AHK script is terminated with
Exit
first — but you can use Task Manager to close the associatedcmd.exe
process.The
Close
menu item as written works fine if there is only onecmd.exe
process. However, if more than onecmd.exe
process is running, this may not properly close the batch file you started with the script (it may close something else). You may want to look into closing by process ID (PID) instead. That said, you can use the same Show/Close/Exit process above as well.Script Notes
refers to the location of a compiled executable with an associated embedded icon file. You should be able to replace this with a reference to any executable with an embedded icon or with a direct reference to an
.ico
icon file as well e.g.