As you've seen, batch files are essentially simple programs, using the built
in command prompt commands as a programming 'library'.
Each line in a batch file is executed sequentially, and they do not have to
be numbered or otherwise identified. The computer simply reads the whole
file from top to bottom and performs any commands the batch file contains.
In addition to the standard command prompt commands that can be used in batch
files, there are a few additional extras like the '@ECHO off' command we used in the last batch file we
created. These can be used to modify how a batch file works. Let's
run through some of them with examples of how they work:
Additional Batch file commands
Can be used to call another batch file from within the current
Used to display information and commands on the screen or prevent
them from being displayed.
causes all commands in the batch file to be displayed
onscreen. This is the default setting.
causes no commands to be displayed. The batch file will
run silently unless you use the echo command specifically as below.
echo what you want on the screen
Causes whatever text comes
after the echo command on the same line to be printed on the screen.
Used to select a specific set of files and run a command on each of
for (variable) in (set of files) do
In another example, the batch file line;
for %%F in (*.txt) do del "%%F"
Gives the variable '%%F' the value of every file ending in .txt in the
current directory, then passes that variable to the DEL command. This
means that every file with the .txt extension in the current directory will be
Moves to different points within a batch
file. The destination point must be indicated with a colon. For
echo this is the end, beautiful friend… the end
Performs a command depending on a condition. IF must include
an ELSE statement which says what happens if the condition is not met. For
if exist c:\myfile.txt (copy c:\myfile.txt
d:\myfiles) else echo myfile.txt does not exist
In this example, if the 'myfile.txt' file exists, it will be copied to
d:\myfiles. If it does not, a message will be shown indicating this.
for more IF command options.
used to create comments, or ignored sections in batch files. The computer
will ignore any line that begins with REM, so you can use this command to add
notations explaining what your file does. For example:
rem this batch file is
rem designed to format
disks in the a: drive