PCSTATS     
[X]   Directory of
Guides & Reviews

Beginners Guides
Motherboards by Brand
Weekly Newsletter
Archived Newsletters

+70 MORE Beginner GUIDES....
Beginners Guides: Understanding and Creating Batch Files
Beginners Guides: Understanding and Creating Batch Files - PCSTATS
Batch files can save time by automating actions down to one simple click. A good understanding of what they are, how they work, and how to create your own, is crucial to today's IT force.
Filed under: Beginners Guides Published:  Author: 
External Mfg. Website: PCSTATS Apr 04 2011   M. Dowler  
Home > Reviews > Beginners Guides > PCSTATS

Anatomy of a batch file

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:

< Previous Page © 2023 PCSTATS.com
Please respect the time and effort that went into creating each PCSTATS Beginners Guide, do not illegally copy. Thank you.
Next Page >

 

Contents of Article: PCSTATS
 Pg 1.  Beginners Guides: Understanding and Creating Batch Files
 Pg 2.  Creating a BATCH File
 Pg 3.  Preparing for your second batch file
 Pg 4.  — Anatomy of a batch file
 Pg 5.  Third trial batch file: getting fancy
 Pg 6.  Batch file error levels and the goto command

 
Hardware Sections 


 
PCSTATS Network Features Information About Us Contact
FrostyTech
PCSTATS Newsletter
Tech Glossary
Technology WebSite Listings
News Archives
(Review RSS Feed)
Site Map
PCstats Wallpaper
About Us
Privacy Policy
Advertise on PCSTATS

How's Our Driving?
© Copyright 1999-2023 www.pcstats.com All rights reserved. Privacy policy and Terms of Use.