Skip to main content

Batch Script Syntax

Batch Script Syntax

Turn off echo

Usually, the first line of a batch file consists of:

@echo off

The command echo off turns off the display for the whole script, except for the "echo off" command itself. The @ sign in front makes the command apply to itself as well.

note

By default, a batch file will display its command as it runs. The purpose of this first command is to turn off this display.

Documentation

Very often batch files also contains lines that start with the Rem command.

This is a way to enter comments and documentation.

Rem This is a documentation line
note

The computer ignores everything on the same line after Rem.

For batch files of increasing complexity, it is often a good idea to have comments.

First Batch Script Program

Open Notepad and enter the following example:

FirstExample.bat
@echo off 
Rem List and save to file all the files in the directory Program files
dir "C:\Program Files" > C:\lists.txt
echo "The program has completed"

The code does the following:

  • Uses the echo off command to ensure that the commands are not shown when the code is executed.
  • The Rem command is used to add a comment to say what exactly this batch file does.
  • The dir command is used to take the contents of the location C:\Program Files.
  • The > command is used to redirect the output to the file C:\lists.txt.
  • Finally, the echo command is used to tell the user that the operation is completed.