Skip navigation.

Tip: A Windows cmd batch file skeleton

This is a work in progresss. Hopefully I'll be able to expand it gradually over a few weeks time.
  • 19 March 2011: Comments
  • 11 March 2011: Sectioning, some code
  • 10 March 2011: Page created

BatchSkeleton.bat: view highlighted, download

Not giving batch programming much attention for a long time, it surprises me what can be done with Batch files or cmd scripts nowadays. Or should I say, Batch file processing is done in surprising ways?

In the past I've written quite a few command line scripts composed of the triple Batch file, Korn Shell script and AWK script. Here the Batch file's only purpose is to start the KornShell script that performs the real processing in concert with AWK and sed. Lateron I used the Z shell in stead of the KornShell.

Then with the advent of Windows XP, things started to break with the Z shell. Also to reduce the dependency on external tools such as the Z shell, I began to re-examine what batch files could do for me. It appeared that quite a few things can be done with the built-in commands such as for in combination with external commands that are available under Windows XP such as findstr.

Writing this article is inspired by the endeavour to create the BuildUniverse.bat Windows cmd build script for one of our projects and reading other's experiences with batch programming, for example the Blog article Windows Batch File Template by Chris Oldwood.

Error messages

Where we're used to page long error messages for C++ templates, cmd is very terse. For example if parentheses are not properly balanced in our long options list, cmd just reports: ) was unexpected at this time. when there are too many closing parentheses. However, if there are too few closing parentheses cmd is not nearly as helpful: The syntax of the command is incorrect. No line number, no indication which command. In other words: The syntax of a command is incorrect. And even an empty line at the right place can lead to an error[x]. You have been warned!

Help and Explanations

Comments and Special Characters

In every computer language I know, comments are stripped during lexical analysis before parsing the remainder as program text. Not so with cmd: rem is a command. Ok. So you can write something like the following (& concatenates commands):

A rem comment can contain almost any text you like. Almost. Quite surprisingly the comment is not completely without side effects. Instead special characters such as > and | are still interpreted, possibly leading to unfortunate behaviour. Of course you can quote them as ^> and ^|, but there is a (partial) solution to this behaviour. Believe it or not, a label is better suited to label something as a comment!

On the one hand :: is seen as a label for a goto command and the line will not be executed. On the other hand a label cannot begin with a colon, so it isn't a proper label either[x]. Note I didn't write perfectly suited. A line with %~ still wreaks havoc. For more information on this behaviour, see [x] and [x].

Quotes and Trailing Spaces

When you use the contents of a variable while chaining another command after that usage, extraneous spaces may be introduced. Attach the & to the preceding variable reference to prevent introducing any traling spaces

This is very useful when returning a value from a subroutine or function, as in line 4 of the following example:

Error Handling and Chaining Commands

Compiler Version Check

In the following example we check for Microsoft VC version 6.

This code is very Microsoft-specfic, generalisation to other compilers is left to the reader. Also a better name for the temporary file should probably be used, e.g by including the date and time in it.

Logging

Persistence

Notes

References and Weblinks

[1] Command Prompt at Wikipedia
[2] Windows PowerShell at Wikipedia
[3] Comparison of command shells at Wikipedia
[4] MSDN Documentation for cmd.exe on Windows XP
[5] Rob van der Woude. Batch files for DOS, OS/2, Windows 95/98, NT 4, 2000 and XP
[5a] Rob van der Woude. Batch files for DOS, OS/2, Windows 95/98, NT 4, 2000 and XP, Comments.
[6] Marc Stern. Accelerating batch files. The 'REM' command.
[7] Timo Salmi's command line interface (CLI) collection with FAQ.
[8] Timo Salmi's command line interface (CLI) collection with FAQ, item 48: Why do some comment lines cause errors? What can I do about it?
[x] for command
[x] findstr command
[Hunt and Thomas, 1999]
Andrew Hunt and David Thomas. The Pragmatic Programmer: From Journeyman to Master. Addison–Wesley, October 1999. ISBN 020161622X.

ToDo

Page created 10 March 2011