404 Tech Support

Advanced Batch Scripting

I’d like to share four utilities that can improve your batch (DOS) scripting by making them more user-friendly, more versatile, and more dynamic. With these tools, you’ll gain a variety of abilities that allows you to do more with batch scripting and make your utilities more polished/enjoyable to the end users.

The four utilities are: Sleep, BATtoEXE, CHP (Create Hidden Process), and CMDOW.

We’ll start off with the most obvious, sleep. Sleep, like the function of the same name in other programming languages, simply tells your machine to idle for a set amount of time.

sleep 10

Tells your script to idle for ten (10) seconds and then resume.

sleep -m 5000

Tells your script to idle for 5000 milliseconds and then resume (equivalent to 5 seconds).

You can get sleep.exe from Microsoft in the Windows Server 2003 Resource Kit.

BATtoEXE also gives a lot of its mystery away in its title. This little tool, unlike the other three listed here, is the only one that you run on your script instead of running from inside your script. BATtoEXE converts your batch script into an executable. This will prevent people from viewing or changing the code and helps establish a ‘final version’ of your script.

BATtoEXE interface

You can choose to have a Console application or a Ghost application. A console application will show the output of your script where a ghost application will invisibly do all its operations in a completely black command prompt window. Don’t use a ghost application if you rely on user input or the window will hang at that point. So, edit your script for any Pause or other input commands before making a ghost application.

You can also use the Include line to bundle a single attachment with your script. You can bundle, mentioned above, sleep.exe and then where ever your new executable script is run, it will have sleep.exe with it.

After compiling your code you’ll have a .exe file so that individuals won’t view or edit the source. It’s also more portable.

The version numbers and other info shows up in the exe

Download BATtoEXE here.

CHP is a simple tool to make your batch scripts more stealthy. Let’s say you need to spawn some processes off during your script to gather information, change permissions, or something like that. If you put CHP in front of the process it will execute almost completely invisibly to the end-user. The only thing that they could see (if they were looking) would be the process in the task manager.

Run another batch script silently:

CHP cmd.exe /c "d:my batch file.bat"

Launch a process like notepad or calculator silently:

CHP calc.exe

Launch notepad and have it print a file:

CHP notepad /p "C:receipt.txt"

This hidden property of the process is also something to be careful of. You want to be sure to close the process or else it will stay open, silently running in the background. Download CHP and see more examples from here.

The final utility is the most powerful and interesting: CMDOW.

You can use CMDOW to interact with windows. You can minimize, maximize, tile, hide, close, and make many other manipulations of the scripting window and other windows open, identifying them by their Handle ID.

You can do a lot of things to affect all windows or the window running the script easily.

To tile all windows vertically:

CMDOW /TV

To minimize all windows:

CMDOW /MA

To maximize the window running the cmdow command:

CMDOW @ /MAX

To mimize the windows running the cmdow command:

CMDOW @ /MIN

Now, if you want to control a different window besides the one running the script, you’ll need to find the handle ID of the currently open process. To do this run:

CMDOW

If you want to narrow it down to just those windows listed in the task bar, run:

CMDOW /T

Lists the windows open in my task bar.

Let’s say I wanted to change the caption of PrimalTask. Look at the row above in the screenshot and we’ll use the handle 0x0503B4.

Another way to get the handle of any windows named PrimalTask would be to use the command:

cmdow PrimalTask

That will pull up only windows named PrimalTask. Put double-quotes around the caption if it contains a space between words.

To rename our PrimalTask window, we’ll use CMDOW again and the handle we found out earlier:

cmdow /REN “To Do List”

Before and After shot of CMDOW Renaming

You might use CMDOW to minimize your script while it’s running and rename the window to display the status of the script as it goes along. CMDOW can be used for a lot of other things as well to help polish off scripts. Read more about it and download it from here.