Tag Archives: .bat

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.

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.

Underused features of Windows batch files

StackOverflow have another interesting topic about: Underused features of Windows batch files.

Of course there are many things I didn’t know about. Some highlights:

PUSHD path
Takes you to the directory specified by path.
Takes you back to the directory you “pushed” from.

By using CALL, EXIT /B, SETLOCAL & ENDLOCAL you can implement subroutines with local variables.


@echo off

set x=xxxxx
call :sub 10
echo %x%
exit /b

set /a x=%1 + 1
echo %x%
exit /b

This will print


even though :sub modifies x.

Creating an empty file:

> copy nul filename.ext

The equivalent of the bash (and other shells)

echo -n Hello # or
echo Hello\\c

which outputs “Hello” without a trailing newline. A cmd hack to do this:

<nul set /p any-variable-name=Hello

set /p is a way to prompt the user for input. It emits the given string and then waits, (on the same line, i.e., no CRLF), for the user to type a response.

<nul simply pipes an empty response to the set /p command, so the net result is the emitted prompt string. (The variable used remains unchanged due to the empty reponse.)

Problems are: It’s not possible to output a leading equal sign, and on Vista leading whitespace characters are removed, but not on XP.

Command separators:

cls & dir
copy a b && echo Success
copy a b || echo Failure

At the 2nd line, the command after && only runs if the first command is successful.

At the 3rd line, the command after || only runs if the first command failed.

Doesn’t provide much functionality, but you can use the title command for a couple of uses, like providing status on a long script in the task bar, or just to enhance user feedback.

@title Searching for …
:: processing search
@title preparing search results
:: data processing

And there are many more. If you want to learn something new, this topic is must have reading!