Mark Needham

Thoughts on Software Development

Spaces in batch scripts

without comments

Since reading The Pragmatic Programmer I’ve become a bit of an automation junkie and writing batch scripts falls right under that category.

Unfortunately, nearly every single time I write one I forget that Windows really hates it when you have spaces in variable assignments, and I forget how to print out a usage message if the right number of parameters are not passed in.

So as much for me as for everyone else, this is how you do it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@ECHO off
IF [%1]==[] GOTO usage
IF [%2]==[] GOTO usage

set VAR1=%1
set VAR2=%2

rem important client stuff

goto end

:usage
echo Usage: script.bat var1 var2

:end
echo Script finished
Be Sociable, Share!

Written by Mark Needham

August 8th, 2008 at 8:10 pm

Posted in Build

Tagged with