Windows batch file scripting notes

Notes on scripting in Windows with batch files.

Windows has a number of scripting methods available. The oldest and simplest are batch files.

As Jim Eshelman says, “The simplest idea of how to write a batch file is: Figure out how you would type the commands at a DOS prompt, then type them, one per line, in a text file — and you’ve written your batch file.”

Having said that, be aware that although batch files originated in DOS they have since evolved to the point that Windows batch files are now considered distinct from DOS batch files. Don’t make the mistake of seeking help with a Windows batch file in a DOS support forum or vice versa. Furthermore, batch file capabilities vary between Windows versions and even among service pack levels. When requesting help, always specify Windows version and service pack level, and state if you require the batch file to run on multiple versions of Windows.

White space is significant in batch files. For example, “set foo = 123″ will set the value of %foo% as ” 123″ with a leading space. Do not use white space unless needed.

Many environment variables are available and can be used as batch file variables. There doesn’t seem to be a complete list of environment variables anywhere, but to see currently declared variables (both environment variables and anything you may have declared), run “set” without any arguments.

Comments are preceded by “REM” or (my preference, for legibility) “::”.

Using environment and user defined variables:

:: Create a new randomly named directory and enter it
set dirname=%random%
mkdir %dirname%
cd %dirname%

To send the output of a command to a file, use the “>” symbol. For example:

:: Log the computer's hostname and users
hostname.exe > hostname.txtdir %SystemDrive%\Users > users.txt

To suppress the stdout and stderr of a command, append “>nul 2>&1” to the end of the command. For example:

:: Copy the current user's example.txt file
copy %appdata%\example.txt" example.txt >nul 2>&1

To launch a program and have the batch file continue to run without waiting for the launched program to terminate, use START. For example:

:: Launch Windows explorer in the Foobar directory of the current device
start explorer.exe "%drive%\Foobar\"

The fuller featured Windows Script Host (WSH) adds (for example) additional options to hide the console window during execution.

Introduction to batch files
Windows command line reference
Windows environment variables (CSIDL values); TODO: Find a newbie-friendly source of this information

Tips and Tricks
Creating a timestamped destination directory


About Warren Post

So far: Customer support guy, jungle guide, IT consultant, beach bum, entrepreneur, teacher, diplomat, over-enthusiastic cyclist. Tomorrow: who knows?
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s