Constructing a batch file

In the following discussion it is assumed that the Introductory page and the page on Commands have been read.

The first line in a batch file often consists of this command @echo off.

By default, a batch file will display its commands as it runs. The purpose of this first command is to turn off this display. The command “echo off” turns off the display for the whole script, except for the “echo off” command itself. The “at” sign “@” in front makes the command apply to itself as well. This nuance isn’t really all that important in the context here but I mention it because it is often seen in scripts. The scripts we will discuss are very brief and omitting this line won’t make any great difference. However, as a matter of good practice, we will enter it in our scripts.

Our first batch file example is going to list all the files in a folder and put the list in a new text file . We will use the directory command “dir” that is discussed on another page. Open Notepad and enter the line “@echo off” (without quotes). Next enter another line dir "C:\Program Files" > C:\list_of_program_files.txt(I’m assuming that your Program Files folder is on the C: drive). This will give us the two-line file @echo offdir "C:\Program Files" > C:\list_of_program_files.txtNow save this two-line file as “listprograms.bat” (without quotes) to some convenient location. Be sure that Notepad is saving as “All files” and not as a text file. See the figure below.

Saving a batch file with correct extension

Three important points are illustrated in the example script. Note that complete paths are used for files including the drive letter. Also note the quotes around “C:\Program Files”. Paths must be quoted whenever a file or folder name has a space in it. Finally note the redirection symbol “>” that is used to send the output to a file instead of the screen.

All that has to be done to use the file is to double-click it. A file C:\list_of_program_files.txt will then be created.


Enables you to include comments (remarks) in a batch file or in your configuration files.


rem [comment]


comment   : Specifies any string of characters you want to include as a comment.

/?   : Displays help at the command prompt.

  • Using the echo command to display comments

    The rem command does not display comments on the screen. You must use the echo on command in your batch or Config.nt file to display comments on the screen.

  • Restrictions on batch file comments

    You cannot use a redirection character “(” or “)” or pipe (|) in a batch file comment.

  • Using rem to add vertical spacing

    Although you can use rem without a comment to add vertical spacing to a batch file, you can also use blank lines. The blank lines are ignored when processing the batch program.


The following example shows a batch file that uses remarks for both explanations and vertical spacing:

@echo off 
rem This batch program formats and checks new disks. 
rem It is named Checknew.bat.
A more general version with arguments

The file that we have been discussing is limited to listing one particular folder and putting the list in one particular file. However, it is easy to make the file able to list whatever folder we want and to put the list wherever we want. Batch files can use arguments or data that is input from the user. The process makes use of placeholders of the form %1, %2, These are replaced in the script by our input data. This type of situation cannot be clicked directly but should be run in a command prompt. The new batch file would be

@echo off

dir %1 > %2Enter in Notepad and save as “makelist.bat”. To run the file, open a command prompt and enter

{path}makelist somefolder somewhere\list.txt


  • somefolder is whatever folder (with complete path) that you want to list in somewhere\list.txt.

Now you have a little program that will list the contents of a folder whenever you want.



timeout: Delay  execution

For example, if the later command need the previous command to be finished completely, you may want the later command to wait for a while, this is why timeout come into our life.

timeout 10
ping -t

In this case, the user can press any key to skip the waiting. You can add /nobreak to prevent this happening.