Friday, February 24, 2012

Windows batch files

Some notes on windows batch files e.g. runnign them in background etc. (start /b cmd ..

for example I use this alot in Grails to start a hsql db instance in the background and the asociated db manager

start /B cmd /c "%JAVA_HOME%\bin\java -cp hsqldb- org.hsqldb.Server -port 9010 -database.0 myDB -dbname.0 myDB"

%JAVA_HOME%\bin\java -classpath hsqldb- org.hsqldb.util.DatabaseManager --url jdbc:hsqldb:hsql://localhost:9010/myDB

All listed here (antak's answer)

(note Call is only needed if you are calling another batch file.. If you have an inline command remove the call)

Built in variables
There are a few build in variables that are useful when writing .bat files

(do a
cmd /?
to get some help on the matter)

%~dp0 lists the current path (d represents the directory, p represents the path)
this is useful if you call a batch file from a different directory.

e.g. cd %~dp0

This however can prove problematic in batch files if you want to append information after the path (e.g. I was trying to append a jar filename e.g.
does not work. (However you can assign it to a variable

set DIR=%~dp0
set FILE=%DIR%/filename

In those cases %CD% is usefule


groovyconsole -cp %CD%\jarfile.jar

This will start the groovy console with jarfile in the path. Useful for tinkering with a new api