Mark Needham

Thoughts on Software Development

Archive for the ‘batch-scripts’ tag

Getting latest tagged revision in SVN from DOS/Batch script

with 3 comments

The way we have setup the build on our continuous integration server, Team City is configured to create a new tag every time the functional tests past successful on that machine.

We then have a QA and Showcase build that we can run to deploy all the artifacts necessary to launch the application on that machine.

Originally I had just written the batch script to take in the tag of the build which the user could find by looking through repo-browser for the last tag created. This quickly became very tedious so I started looking for a way to get the latest tagged revision from the command line.

We thought it would be possible to get this information using svn info but it turned out that the information returned by svn info about revisions doesn’t necessarily refer to the latest created tag. We ended up using svn log and then parsing through that data. It’s a bit messy but it does the job (I name each tagged version of the code as ‘build-{TeamCity-Build-Number}):

1
2
FOR /F "Tokens=2" %%i in ('svn log /tags/path --limit=1 -v ^| find "build"') do set TMP=%%i
FOR /F "Tokens=2 delims=/" %%i in ('echo %TMP%') do SET TAG=%%i

The for loop uses a space as its default delimiter so that’s what the ‘delims=/’ is doing on the second line, the ‘Tokens=2’ allows us to get the second token after the string is split and the ‘^’ in the first command is being used to escape the pipe.

Written by Mark Needham

August 16th, 2008 at 12:10 am

If Else statements in batch files

with 14 comments

I mentioned in a couple of earlier posts that I’ve been doing quite a bit of work with batch files and the windows command line, and today I wanted to do an If Else statement in one of my scripts.

I thought it would be relatively simple, but after various searches and having read articles that suggested that there wasn’t an ELSE construct in batch land I finally found a forum post which explained how to do it.

The script I’m working on takes in a working directory as one of the arguments and what I wanted to do was either set a variable to be the value passed in, or if the value passed in was ‘.’ then to set it to the current working directory.

1
2
3
4
5
IF "%1"=="."  (
  set WORKING_DIRECTORY=%cd%
) ELSE (
  set WORKING_DIRECTORY=%1
)

I played around with this a little bit and it does seem that the brackets need to be in that exact format otherwise it doesn’t work at all. Even putting brackets around the IF part of the statement will stop the script from working as expected.

IF statements on their own are much easier to deal with. To check for an empty argument for example either of the following will work:

1
2
3
IF "%1"=="" GOTO usage

IF [%1]==[] GOTO usage

It does all seem a bit fiddly and Powershell is probably the way forwards, but for now batch scripts it is!

Written by Mark Needham

August 13th, 2008 at 10:27 pm

Getting the current working directory from DOS or Batch file

with 23 comments

In the world of batch files I’ve been trying for ages to work out how to get the current/present working directory to make the batch script I’m working on a bit more flexible.

In Unix it’s easy, just call ‘pwd’ and you have it. I wasn’t expecting something that simple in Windows but it is! A call to ‘cd’ is all that’s needed. If you need to set it in a batch script the following line does the trick:


set WORKING_DIRECTORY=%cd%

I was surprised that something so simple (I do now feel like an idiot) wasn’t easier to find on Google. I ended up going via Experts Exchange (how they end up with such high search results when you have to pay to see the information is beyond me) and several other verbose ways of solving the problem before finally coming across this article which explained it.

Written by Mark Needham

August 12th, 2008 at 10:37 pm

Posted in Batch Scripting,Build

Tagged with ,

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

Written by Mark Needham

August 8th, 2008 at 8:10 pm

Posted in Build

Tagged with