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.


Fun with Arduino and AtTiny!

Lately I’ve been playing with the Arduino microcontroller and I have to say I really like it.  You can find it at Radio Shack for less than $40, and it comes with the board and a USB cable.  There is a free IDE that makes it fast and easy to get started blinking LEDs, no soldering required.

My first (interesting) project was to create laser tag guns.  We went to a laser tag arena about two weeks ago, and I thought this would be an interesting project.  There are several examples of arduino laser tag, from a commercial site to and IBM project walkthrough, but I decided to go cheap and build it myself.

And when I say cheap, I mean CHEAP! I found some paired-down chips that can be programmed with the arduino at the site for a total of 2 bucks each.  Radio Shack has emitter/receiver pairs for $4, and my kids made lego gun cases for free (and they let me help).

I found some small breadboards on Ebay for $1.25 including shipping, and I had some resistors and LEDs laying around for misc. parts.

I’m still learning about programming the Attinys but I’ve got the process down and the parts, so I think I just need some time to put it together.

One of the interesting problems I found is that the Arduino can communicate back and forth to the IDE via the Serial Monitor.  Values on the chip can be sent to the screen, so you can tell what’s going on.

With the AtTiny is that it doesn’t have a connection to the PC, except through the Arduino, so it’s hard to debug what’s going on.  I used a couple of work around to get the serial interface to go from the Tiny to the Arduino to the computer.

1.  Connect the arduino.  Disconnect the capacitor to the reset (if connected).

2.  Upload ArduinoISP

3.  Connect the capacitor to reset, and pins 10-13.

4.  Load the code you want to test on the tiny.  I cobbled together this
#include <SoftwareSerial.h>

SoftwareSerial mySerial(1,0); // RX, TX
const int led = 4;

void setup()
pinMode(led, OUTPUT);

void loop()
for (int i = 0; i <= 5; i++)
mySerial.println (i); //Send i to Rx Arduino
digitalWrite(led, HIGH);
digitalWrite(led, LOW);

5.  Disconnect the capacitor and pins 10-13.

6.  Change the board to the Arduino Uno

7.  Upload the following code.

Software serial multple serial test

Receives from the hardware serial, sends to software serial.
Receives from software serial, sends to hardware serial.

The circuit:
* RX is digital pin 10 (connect to TX of other device)
* TX is digital pin 11 (connect to RX of other device)

Not all pins on the Mega and Mega 2560 support change interrupts,
so only the following can be used for RX:
10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69

Not all pins on the Leonardo support change interrupts,
so only the following can be used for RX:
8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).

created back in the mists of time
modified 25 May 2012
by Tom Igoe
based on Mikal Hart’s example

This example code is in the public domain.

#include <SoftwareSerial.h>

SoftwareSerial mySerial(11, 12); // RX, TX
int led = 13;

void setup()
// Open serial communications and wait for port to open:
digitalWrite(led, HIGH);
digitalWrite(led, LOW);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only

Serial.println(“Goodnight moon!”);

// set the data rate for the SoftwareSerial port
//mySerial.println(“Hello, world?”);

void loop() // run over and over
if (mySerial.available())
if (Serial.available())


8.  Open the serial window (ctrl-shift-M) and watch the communication work!


AutoHotKey – My go-to automation tool

I spent a lot of time programming, and sometimes that means retyping the same thing.  For instance, when I’m in the database, I type ‘select * from ‘ dozens of times a day.  Instead, I can type s*f and autohotkey erases that and puts in ‘select * from’.

SendInput select * from

This works in any window, not just my sql editor.  I have about a dozen auto replace commands that I use all the time, and some of them type in 20+ lines of sql for me.  This 6 character command


Generates this sql

–Report how long queries take
select operation_name,(percentile_cont(0.90) within group (order by duration))/1000 Percetile_90,
(percentile_cont(0.99) within group (order by duration))/1000 Percetile_99,
(max(duration))/1000 max_duration,
(min(duration))/1000 min_duration,
from audit_service aud
where source_application = ‘SLDB’
and aud.CORRELATION_ID in (
select aud.CORRELATION_ID from audit_service aud
and aud.OPERATION_NAME in (‘validateServiceAvailability’)
and aud.AUDIT_TIMESTAMP > sysdate – 10/1440 )
group by operation_name
order by max_duration desc

That’s a pretty good return on investment!

You can also set hot keys to perform actions, just like on fancy multimedia keyboards.  If you’re not using F8 through F12, use those for pause, play, fast-forward, and rewind.  Or Open Chrome, Open IE, Open Firefox, Open Gvim, if you’d rather.

There is even a language for setting up GUIs and programming your own applications, which I haven’t gotten into.  In my next post, I’ll show you how to navigate your web application so you don’t have to keep clicking the same things over and over.

So, if you’re interested in being more productive, making fewer mistakes, and generally being a happier developer, check out AutoHotKey.


Tools, Jigs, and Devices

One of the things that drives me crazy is that a lot of software developers don’t write software to make their jobs easier.  Whether it’s testing code, monitoring environments, or researching bugs, there are lots of ways to automate your way out of a boring job and look like a genius.  It’s work, but it makes it easier in the long run.

In other jobs this happens all the time.  Carpenters know that creating a simple tool (called a jig) can save a time and increase quality.  Even street sweepers have a stick with a nail in the end.  But for a Java developer, when they need to do something that takes 7 separate steps, you’re lucky if they’ll write them all down, much less make a tool to do it for them.

The first time I tried this, I automated the build for a software project.  It was a classic situation where there was only one person that could build the project, and it took lots of specialized knowledge, and it only got done about once a week.  If you didn’t get something into that build, you’d have to wait another week until the next one.

Fortunately, I had just read Jared Richardson’s Ship It, which talked about repeatable builds.  I automated the download and build out of the SCM, and made it so anybody would be able to build.  My boss said that was the best process improvement he’d seen in his career.  Of course, he was the one that spent a half-day each week putting together the build, so he was the happiest person of all.

Shortly thereafter, I increased the number of tests we were doing for an upgrade from 12 (two tests per person, six developers, all manual) to over 8,000.  We found a ton of bugs that traced back to the same cause, and a lot of little bugs that were special cases.  None of those were covered in the original 12 test cases.  In the end, the upgrade went smoothly and page generation time went from 3 minutes to 15 seconds.

One great book about automation is, The Productive Programmer by Neil Ford.  I save myself 3 hours a week by taking some of those ideas and putting them into everyday life.  Especially:

  • Know your IDE
  • Create your own launchpad
  • Automate repetitive tasks

It’s 5 years old, but the techniques are more about how to be productive, so they are still relevant.  If you were only going to read one book to make you a better programmer this year, I’d make it The Productive Programmer.

And there are other tools you can use to make things easier.  I’ve started using Maven for builds, because creating new boilerplate apps is fast and easy.  Launchy is a little software device that lets you type in what program to run, and even corrects your spelling.  Using documentation to document things you really need is a tenant of Agile development, but you’d never know it from how most people do Agile.  Saving those documents forever means I usually never use them, except for about twice a year, when I need something I thought I was done with.

Think about some things that you do that you can automate.  You might be surprised how much you can get done when you don’t have to do it yourself.


New Computer Software Install List

I recently got a new computer for work, and it’s taken a while to get all the tools I’m used to.  Here are the things I’ve missed in the last week in approximate order.

  • Launchy – keyboard launcher so I don’t have to click to find my apps.
  • CLCL – Clipboard manager so I can find things on my clipboard without having to copy them again.
  • UnixUtils – Windows commands that give you handy unix utilities: ls, find, grep, etc.
  • GVIM – Windows, Linux, and Mac compatible super-VI editor.
  • AutoHotKey – Remaps your keys to different keys, or provides global key shortcuts.
  • PuTTY –  Telnet and SSH client.
  • Cropper – Screen capture tool that goes to a file OR your clipboard.
  • Winamp – Music player with great radio stations and visualizations.
  • VLC – Video Lan Client plays almost any video file.
  • Virtualbox – Virtual machines are SO handy.
  • WinSCP – Windows-friendly tool for copying files to and from remote servers.
  • SoapUI – For hitting web services and parsing WSDL.  Plus, the latest version has a copy of TCPMon, which is like a wiretap for TCP communications.


There are also the different versions of Java and Ruby that are handy to have around.  In order to keep those straight I try to make a setupXXX.bat in the c:\ folder to set the environment variables for each.  That way each time I start a command window I can set the environment to what I need.

One tool I’ve always liked, but never have been able to use with discipline, is TiddlyWiki.  It’s like a wiki, but it runs locally in your browser.  It seems like it would be very handy for keeping track of lists, but I always use paper notebooks to keep my lists in, so I have at least ten separate versions that I start and don’t update.

Maybe now that I have a list where I can find it, I will be able to set up my next computer even faster.

Kickstart Java Projects with a Simple Ant File

A couple of times a year, I think about starting a new Java project.  Usually it’s a tool to automate tasks that I’m tired of doing manually.  It often involves parsing a file, or testing a web service, or accessing a database.

For a while I would write it in Ruby, but it was always a pain to distribute.  First, you’d have to see if the other user knows what Ruby is, tell them to get it installed, etc.  Plus, in my organization, ruby isn’t a supported platform, so these projects never have a chance to get maintained or updated unless I do it.

I’m just enough of an automation geek to think that all projects, even these little ones, need to have a build system.  That way if I do want to update it I can do it without having to remember how to set up the class path, etc.  And I don’t like using IDEs for builds.

What I really wanted was the convenience of the Rails new project with the acceptance of the Java language.  After fiddling around a bit, I decided to write my own.

It’s just one ant file.  It creates the directories, compiles the files, and jars them up for you.  It keeps old versions of the jars with date-time stamps, but copies the latest version to the build.xml directory so you can easily run it.  It can run junit test, and it even includes instructions on how to compile your first code and test classes.

If you need a simple way to start a small Java project, you can check it out.


December Updates

Updates for December:

  • Just missed the Triangle Arduino meeting after staying up all night rolling out new software.  I hear there’s a new Arduino kit under the tree, though.
  • While Setting up for our home schooler, I found is available.  I bought it, but I’m waffling on whether to move the email and so forth over.

More to follow,