Double-click a Bat File: Hide Window, No Console, Silent
by Mike Levin SEO & Datamaster, 07/29/2010
Call it what you will, it’s a pain to get rid of that CMD window that pops up when you double-click a .bat file. This post will address how to do that so we can elegantly launch our the ideal Linux USB pendrive with a simple icon double-click, whether we’re on a PC or Mac.
We’re sitting on top of a Mac OS X Application Bundle (which we got to work) sitting on a USB pendrive, that we’re copying back to a PC. The directory structure and file naming established on the Mac is very particular and is required to maintain its integrity as an application bundle.
Thankfully on the PC, all of QEMU is just dumped into a single directory, and shell scripts, or .bat files in this case, start in the directories they’re double-clicked from. So, we can make a double-click launch icon on the PC much more easily than on Mac OS X.
However, the PC is not without its gotcha’s, and in this case, it’s an extra “locked-up” DOS CMD window that doesn’t go away while your VM’s running. To see it, let’s figure out how to get the harddisk.raw burried in Debian.app running on a PC without disturbing any of the Mac files, and while still treating the Debian.app as much like a single-file program as we can.
To that end, actually drag-copy the qemu directory that you were working with previously on the PC to be INSIDE Debian.app. In fact, go so far as to drop it all the way inside the Resources directory… but wait! There’s already a directory named qemu in there, so let’s rename our original qemu directory to pcqemu, and put that alongside the existing qemu directory in Debian.appContentsResources This shuld minimize the chances of the presence of these PC files disturbing the Mac Application Bundle.
Now that files are in location, let’s see if we can get qemu on the PC to run from this new location. We’re dealing with some very long paths now, so I’m going to share a trick so that you will never have to type a long path for your starting directory again.
You can usually do this by opening any Windows Explorer window and pasting %SystemRoot%system32 into the address bar, and then scrolling in the window until you find cmd.exe. Instead of scrolling, you can just start typing “cmd.exe” and it should jump you there.
Now, right-click-and-drag cmd.exe onto your desktop and on the context-sensitive popup menu that appears, select “Create shortcuts here”. You will now have a shortcut to your desktop to cmd.exe. If you were to just double-click on it, then it would open a DOS CMD window with the current directory set to something like C:Windowssystem32. But that’s exactly what we don’t want. So right-click on the shortcut and select Properties. On the resulting requester, in the “Start in” field, replace “C:WINDOWSsystem32” with “%CD%” (minus the double-quotes, of course).
Now you have a shortcut to open a DOS CMD window that will have it’s current directory set to wherever the shortcut was double-clicked from! This is a huge time-saving tip.
I bet you can guess what’s coming next. That’s right! Drag-copy this shortcut into Debian.appContentsResourcespcqemu If you double-click it, you will have a new CMD window with the current directory set to this location–very convenient for the qemu command we need to type now:
qemu -hda ../Guest/Debian.qvm/harddrive.raw
You may remember the middle DOS window from the earlier steps where we were setting up the VM, and not thought much of it. Now here’s where it bites us. We can easily make a .bat file that automatically does what we just did manually, bit it will be pretty darn ugly to have this locked-up DOS window hanging around every time we want to run our virtual machine on the PC.
So, here’s what we do: create a new .bat file in the pcqemu directory. My preferred way to do this is to hit Windows-key+r which gives us the type-in command prompt. Type notepad and hit enter. This will pop up the Notepad.exe program on every PC. And now type:
./qemu -hda ../Guest/Debian.qvm/harddrive.raw And now select “Save as” and surf into your pcqemu directory, and save it as qemu.bat Double-clicking this will have much the same effect as running it from the command-line, producing the DOS lock-up window. Now, using that same notepad window, type the following:
Set WshShell = WScript.CreateObject(“WScript.Shell”) Return = WshShell.Run(“.qemu.bat”, 0, True) set WshShell = Nothing
That makes it a Visual Basic Scripting Edition (VBScript) file that is executable by the Windows Script Host (WSH) built into every modern copy of Windows. The “0” parameter on WshShell.Run is an intWindowStyle that tells it to close the current window and open another (exactly what we need), and the False is a bWaitOnReturn parameter telling the VBScript that it doesn’t need to bother waiting around for the DOS command to finish before it continues executing, and since there’s nothing else to execute, the memory of firing up WSH is released, and we have a clean, extra-window-free VM launch.
You can go ahead and click qemu.vbs to test it. See? No console appears. Thanks to a VBScript launcher, we hide the window and have run it silently. Nice, huh? We’re almost there. Only a Windows shortcut and pretty icons to go.