What is a batch script?

This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed).

Previous article: MS-DOS (cmd.exe) command prompt: CD, MD, copy.
Next article: First cmd / DOS batch script example.

Batch scripts is still relevant in 2014. Batch programming is repeatedly used in Windows server and workstation administration to make recurrent tasks easier and more efficient.

In the MS-DOS, Windows 95 and Windows 98, and nowadays in the DOSBox the most popular batch file is autoexec.bat. This file was appropriately named after AUTOmatic EXECution. Commands that are to be executed when DOS starts are placed in this special file particularly on systems that utilize the PROMPT and PATH commands. When pure DOS was the prevalent operating system this was important, however, on our newer Windows guided machines, this is really not of concern. The PATH environment variable can be modified from the System properties – Environment variables.

Environment variables - PATH - Windows 8.1
Environment variables – PATH – Windows 8.1

For creating and editing a batch file, I recommend to use Windows Notepad, or more advanced editor with syntax highlighting support, such as free and open source editor – Notepad++. You will save your batch files with a .bat or .cmd file extension. Some of the file naming conventions (requirements) for batch files include: not recommended to use any internal DOS commands for file names for example, ECHO.bat, ERASE.bat, PAUSE.bat, etc., and SPACES, and device names such as LPT, COM1, COM2, NUL, etc., and it is NOT OKAY to use the following special characters in a filename:

  1. \ – Backslash
  2. / – Slash
  3. : – Colon
  4. ? – Question mark
  5. * – Asterisk
  6. ” – Quotes
  7. < - Less than sign
  8. > – Greater than sign
  9. | – Pipe

The period must be used as the separator between the file name and the file extension. The colon is specifically used after a letter to identify a particular disk drive. The asterisk and question mark are special wild card characters. Be careful when using these two characters in your script as they can act on multiple files / folders, and with not planed carefully, do some very unpredictable things. The greater than sign, the less than sign and the pipe are special DOS commands to be discussed in the next articles. They are also illegal file names. When naming a DOS file you have even more restrictions.

It should also be noted that good recommended practice for batch file names is to not save them as the same name as another executable file with a different extension. If you already have a file named email.exe do not try to save the batch file as email.bat.

The format for batch files is a series of DOS commands on separate lines. Example:
First Command
Second Command
Third Command
Fourth Command
And so on...

Four types of commands are acceptable for a batch file. These are:

  1. internal commands
  2. .exe program names
  3. .com program names
  4. .bat / .cmd file names

As with any DOS procedure you can stop a batch program from executing by pressing CTRL+C or CTRL+Break key combination on the keyboard. DOS will ask if you wish to Terminate batch job (Y/N)?. Type N to skip the command you broke out of and proceed to the next command in the batch file. Press Y to abort execution of the batch file and return to the command prompt. Pressing ^Break will produce the same results as ^C (except some commands, like PING).

The last command that we will go over in this section is the VER command. VER stands for VERsion. Enter this into the command prompt and you will be presented with the current version of your DOS program / Operating System.
C:\>ver

The VER command displays the operating system version number
The VER command displays the operating system version number

This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed).

Update Oct 6, 2014: added link to the next article.