The scope of this document is my usage and knowledge of the Batch scripting language. It’s by no means a complete reference of batch scripting. A simple Google search returns millions of complete documentation websites. The reason for this specific page is to 1. For me to solidify my memory 2. Help the reader to understand other given examples in the Batch Scripting category in this website.

Table of Contents

1. What are Batch files

According to my usage, batch files are just scriptable shortcuts that can do more than just running a program or execute a single command prompt command.

My example usages:

  • Run multiple programs with a single shortcut
  • Create shortcuts for command line tools for frequently used paths
  • Log the results of a command line tool into a text file

2. Creating batch files

Create a txt file, rename the extension to bat. Alternatively in any text editor create a new text file, save it with the extension .bat.

Some text editors offer syntax highlighting for batch files. My preferred editor is Sublime Text 3.

3. Commands

  • Any command when followed by /? will display the usage information (e.g. ECHO /?)

  • ECHO Is used to print (echo) a literal value or a variable in the command prompt. (e.g. ECHO Hello World) ECHO when used with the verbosity switch @ can be used to toggle printing of execution of a batch script to the command prompt console. Has two possible values ON and OFF, default being ON. (usage: @ECHO OFF) ECHO without a following variable or a literal prints it’s status to the console, whether it’s ON or OFF. To print a blank line ECHO should be followed by a dot character (ECHO.).

  • REM discards the line execution of the line after it, it’s generally used as a mechanism for comments. It can also be helpful as a debugging mechanism, to inspect the behavior of the script when some lines are disabled. (e.g. REM ECHO Hello World )

  • TIMEOUT will pause the execution of the remaining lines after it for a given amount of seconds or until user input. When used in the form TIMEOUT 10, any user input will cancel the timer and the script will resume. TIMEOUT -1 will only wait for user input, without any timers. An optional parameter called NOBREAK will only resume the rest of the script if the user cancels the TIMEOUT program with the CTRL+C shortcut, no user input will break the timer (e.g. TIMEOUT 5 /NOBREAK).

  • START starts a program or a command in a separate window. Has a ton of parameters, use /? for more info.

  • SET displays, sets or removes environment variables for the current command prompt session. Usage is in the form SET variable="value" Multiple environment variables can be set when separated by the character ; (e.g. SET path="D:\folder 1";"C:\folder 2)

%path% is an internal variable which holds all the environment variables set in the variable “path”. It is customary to add it to the end of any SET command as to not lose all the previously set PATH environment variables in the execution lifetime of the batch script.

Surround your string values with " whenever there is a whitespace character in them or the values after the whitespace won’t be processed. It’s a good practice to use double quotes all the time as it makes it easier to distinguish values from the rest.

  • ^ character can be used to write your commands in multiple lines. Use it at the end of a line like:
  • PAUSE can be used to wait for user input to continue execution.