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
Call
Can be used to call another batch file from within the current
one
call c:\batchfile2.bat
Echo
Used to display information and commands on the screen or prevent
them from being displayed.
Echo on
causes all commands in the batch file to be displayed
onscreen. This is the default setting.
@Echo off
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.
For
Used to select a specific set of files and run a command on each of
them.
for (variable) in (set of files) do
(command)
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
deleted.
Goto
Moves to different points within a batch
file. The destination point must be indicated with a colon. For
example…
goto end
:end
echo this is the end, beautiful friend… the end
If
Performs a command depending on a condition. IF must include
an ELSE statement which says what happens if the condition is not met. For
example:
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.
Look here
for more IF command options.
REM
Rem is
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:
@echo off
rem this batch file is
rem designed to format
rem floppy
disks in the a: drive
format a: