5

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?

2

3 Answers 3

3

Since I don't believe this can be done through a simple .bat file, this seem like a job for AutoHotKey.

AutoHotkey (AHK) is a free, open-source macro-creation and automation software for Windows that allows users to automate repetitive tasks. It is driven by a scripting language that was initially aimed at providing keyboard shortcuts, otherwise known as hotkeys, that over time evolved into a full-fledged scripting language.

According to this forum post from 2014 you should be able to toggle window visibility with a script similar to this:

#NoTrayIcon
#Persistent

global hBatFile

/* Setup Tray icon and add item that will handle
* double click events
*/
Menu Tray, Icon
Menu Tray, Icon, C:\windows\system32\cmd.exe
Menu Tray, Add, Show / Hide Pingu, TrayClick
Menu Tray, Add, Close Pingu, CloseItem
Menu Tray, Default, Show / Hide Pingu

;// Run program or batch file hidden
DetectHiddenWindows On
Run pingu.bat,, Hide, PID
WinWait ahk_pid %PID%
hBatFile := WinExist()
DetectHiddenWindows Off
return

TrayClick:
OnTrayClick()
return

;// Show / hide program or batch file on double click
OnTrayClick() {
    if DllCall("IsWindowVisible", "Ptr", hBatFile) {
        WinHide ahk_id %hBatFile%

    } else {
        WinShow ahk_id %hBatFile%
        WinActivate ahk_id %hBatFile%
    }
}

CloseItem() {

       DetectHiddenWindows On
       WinWait ahk_class ConsoleWindowClass
       Process, Close, cmd.exe
       DetectHiddenWindows Off
       ExitApp

}

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:

  • pingu.bat is the (arbitrary) name of the batch file we wish to run
  • hBatFile is an arbitrary variable name holding some window information
  • Pingu is an arbitrary name which shows in the additional AHK tray menu items

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 below Show / Hide (Exit will only close the script, not the console window).

Also note that

Run pingu.bat,, Hide, PID

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 as pingu.bat or that pingu.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 replace pingu.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 stop cmd.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 script

They 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 associated cmd.exe process.


The Close menu item as written works fine if there is only one cmd.exe process. However, if more than one cmd.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

Menu Tray, Icon, C:\windows\system32\cmd.exe

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.

Menu Tray, Icon, C:\Path\To\Icons\icon.ico
4
  • I followed your instructions, and came up with an AHK script, but I've run into another issue. I've asked about it on the AHK forums; do you think you could check it out? autohotkey.com/boards/… Jul 30, 2016 at 8:58
  • You can see there the whole script I'm using. The problem is that the AHK script is getting orphaned, not the console window. Jul 30, 2016 at 9:09
  • Nevermind, the issue's gone away. Thank you for your very detailed answer :) Jul 30, 2016 at 14:54
  • @NicholasRichards Your welcome for the answer. I am glad you got it working the way you like. :) Jul 30, 2016 at 22:00
3

How do I make a *.bat file run in the tray?

You can use RBTray:

RBTray is a small Windows program that runs in the background and allows almost any window to be minimized to the system tray by right clicking its minimize button.

It is free, open source, and is distributed under the terms of the GNU General Public Licence.

It is only 45k and has both 32 and 64 bit versions.


Disclaimer

I am not affiliated with RBTray in any way, I am just an end user of their software.

4
  • I use this software too, it's easy to use, just right click on the minimize button and it's in your tray, works with almost every program!
    – Alex
    Jul 29, 2016 at 12:45
  • Is it possible to get this to do that automatically whenever a particular window/program opens? EDIT: Nevermind. I just checked, this does not work with cmd windows. Nice software, though. Jul 29, 2016 at 20:44
  • @NicholasRichards No idea. I've never had a need to automate that. Maybe AutoHotkey or similar could do this ...
    – DavidPostill
    Jul 29, 2016 at 20:47
  • RBTray cannot minimize elevated command line windows (ran as admin, which is most of them) Mar 15, 2022 at 10:51
1

You can use the open source tool MoveMouse which allows to run mininized in SysTray and easily allows to run Batch scripts on a scheduled or event based way.

Its original purpose is to move the mouse pointer a little bit to prevent application/or system actions (such as entering away status in chat programs or running screensavers during presentations) based on inactivity.


Setup:

To set it up, open the application, then click on the "Scripts" tab, there you have the option "Execute script at each interval": Import the (power shell) script you want to run via the Import button and then click the Start button. The interval time can be set on the "Actions" tab, specified in seconds.

Via the "Schedules" tab you can also set up when to start/stop running scripts based on a time schedule. If needed, you can also set up black out periods by entering a start and stop time on the "Blackouts" tab, blocking execution of scripts during that time.


Note: Since MoveMouse only supports PowerShell scripts, if you need to run a batch file you can invoke it as start-process C:Pathfile.bat or as start-process "cmd.exe" "/c C:Pathfile.bat", as described here.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .