Batch File Programming for Windows

Writing Scripts for Execution from the Windows Command Line

How-to article to help the reader get started with Windows command line programming, using batch files, and solving problems without programming in the traditional sense.

This article is designed to get the reader started with batch programming under the Windows command line environment. Command line programming is very useful because it allows the programmer to have access to the tens of commands available from the command line, as well as some proper programming structures for:

  • decision making
  • loops
  • passing commands
  • parsing files...

For example, it is possible to test values that are fed in from the command line, as well as parsing an entire file and performing commands on each token extracted from it. These are tasks usually handled by a higher level software application programming language such as C++ or Java. The batch programming language is rich enough to perform many functions, albeit sometimes in a cumbersome and inefficient manner.

It is, however, perfect for automating command line tasks (backups, copying files, ftp uploads, etc.) and basic editing jobs (adding headers and footers to HTML, publishing thumbnail directories etc.).

For a general overview and first taste of command line programming under Windows and DOS, the following article introduces the topic:

Each file is a plain text file, with the extension .bat, containing one or more commands from a simple 'language'.

Commands

The following commands are available for use in batch file programming:

  • Screen output : ECHO
  • Input : PAUSE
  • Flow Control : CALL, GOTO
  • Conditional processing : IF
  • Repetition : FOR
  • Parameter passing : SHIFT and the %0 to %9 variables
  • Environment handling : SETLOCAL, ENDLOCAL

There are no commands for receiving ad-hoc input from the user (the PAUSE command just waits for a keypress), nor any that allow interactivity in terms of mouse handling. For this reason, batch files are typically just used for non-interactive automated tasks.

The following is a list of articles related to individual batch commands:

Typically, layers of commands are built up consisting of batch files calling each other. Recursion is allowed (scripts calling themselves) with the usual caveat that an exit strategy must be provided for.

Tips & Tricks

Despite the primitive origins of batch file programming, the language can be persuaded to perform some complex tasks. These are usually supported by the so-called command extensions which are enabled under Windows XP by default.

The following articles highlight some of these advanced uses:

Guy Lecky-Thompson, Self Portrait

Guy Lecky-Thompson - Guy W. Lecky-Thompson is the author of several technical and non-technical books, and writer at large. He has written for Dr. Dobbs ...

rss
Advertisement
Leave a comment

NOTE: Because you are not a Suite101 member, your comment will be moderated before it is viewable.
Submit
What is 7+3?
Advertisement
Advertisement