How to make your life easier: Custom batch file

I constantly look for ways to make my work more productive.  One of the things I like to do is make batch files to string together commands that have to be done in order.

The Problem:

On one project, I have to do this 5-30 times a day:

Compile the code

Stop the server

Deploy the project to the server

Start the server

However, about once a week the temporary directory gets corrupted.  Sometimes that takes HOURS to debug.  Also, checking out a new branch sometimes points me to the wrong environment and eats up time, too.  To avoid it I just overwrite these files each time I deploy.  But now I do this 5-30 times a day:

Compile the code

Stop the server

Clean out the temp files

Copy the configuration files

Deploy the server

Start the server

How can we make this easier?

The Solution:

I have a directory that is on my PATH (I work in Windows, but Linux would be the similar) that holds all my batch files.  They are short and all follow the same format:

  1. Set up the environment
  2. Do the work
  3. Show that you are done

1.  Setup

Setting up the environment doesn’t seem like a big deal at first.  You know what version of Java you’re using, where tomcat is installed, and what ant version to use, so you can just hard code those paths.  However, it only takes a few extra minutes to set up a file that sets environment variable that you can use (and re-use) in other batch files.  I set tomcat install directories, useful code directories, and add directories to other useful tools.


set JAVA_HOME=C:\bea9\jdk150_04
set ANT_HOME=C:\apps\ant\apache-ant-1.6.5
set ANT_OPTS=”-Xmx512M”
set PATH=%ANT_HOME%\bin;%JAVA_HOME%\bin;%PATH%
set PATH=C:\work\bin;%PATH%
set SVN_EDITOR=c:\windows\system32\notepad.exe
set TOMCAT_HOME=c:\apps\Tomcat7.0.27
set AP=\work\awesomeProject\version_3\repo

now if I run \work\apsetup.bat I can type

cd %ap%

instead of

cd \work\awesomeProject\version_3\repo

However, if I want to change %ap% to a different version, I can change the last line “set AP=…” to that directory.

2.  Do the work

Now that I have the environment set up, I can call %ap%\compile.bat ore %ap%\runTests.bat from the command line window.  Or add this like to skip %AP%\ each time:

set PATH=%AP%\bin;%PATH%

Do the work

Once your environment is set, it’s handy to use absolute paths to show which programs.


Where compileAP can

Kill the server

Compile cleanly (and remember all the options)

Remove temp files


Or %AP%\bin\bounce.bat can

Kill the server

Clean the temp files (but not the deployed ones)

Start the server

3.  Show you’re done

Sometimes it takes a while to complete all these steps.  If distractions occur it make be a while before I get back to my tasks.  It’s frustrating to lose track of where you are and have to start over.

To solve this, I try to do two things:  Give a status update with a timestamp, and give an audible cue when I’m done.

For timestamps I like to use the tools at , which give you unix-style commands from the windows command line.  Most of the useful commands are there, and I use them all day. It’s easy to put a “date” command at the end of a long process, so I know when it got done.  Also, using the tee command (split output to the stdout and a file) is super handy if we need to see what happened when.

The other timesaver is to send a beep command to the standard out.  For long running commands this reminds me to stop writing email and get back to work.  In windows this can be tricky, but the easiest way I’ve found is to type this in a directory on your path

copy con beep.bat

Then type control-G control-Z.   This gives you two beeps, no muss, no fuss.