Introduction To Batch Files
Introduction To Batch Files
-1-
Introduction to Batch Files
By Acid Crasher X
Content
Introduction
Basic Commands
-2-
Introduction
A batch file is a file by which you can instruct the operating
system to perform a task or action such as: copy, delete, start,
format, etc.
When a batch file is opened, Windows’ Command Prompt opens
up and performs the tasks that are scripted or written in to the
batch file.
In some ways, you could say that batch files are dangerous because
they could slow down your computer or they could delete
everything on it, also called formatting.
A batch file is also able to delete Windows’ configuration
components which could alter the way Windows works or it could
make your computer not operate properly.
When creating a batch file, you must be careful with what you
script, because when deleting a file or formatting a disk, there is
no turning back. The file or disk is deleted permanently. In
some occasions, when using the command prompt, it will prompt
or ask you if you are sure that you want to proceed with the action.
In most cases, it doesn’t.
-3-
The Command Prompt
The command prompt is a tool by Microsoft in which you can
instruct the computer to perform a task. Batch files are based on
the command prompt, except that batch files are many commands
at once scripted into a file.
The command prompt looks something like this…
This is what the application would look like on the start menu:
-4-
The second way to open it would be to use Windows’ Run
application. You can open run by going to the start menu, there the
application will appear.
Or you could press Windows Key + R. In the Run box, type cmd
which is short for “Command”.
-5-
When opening the command prompt, there are many things you
can do. One of the problems with the direct use of the command
prompt is that you can’t paste text into it. If you try to it would
look something like this.
In this manual I will use the command prompt to show you how
the scripted command would be displayed. I will not show you
how to do things directly from the command prompt because this
is not a manual about the command prompt; it’s a manual about
batch files.
-6-
Basic Commands
To be able to create a batch file, you must first know the
commands you need to use along with the appropriate syntax.
The syntax of a command is everything you write after the
command. For instance, if you want to shutdown your computer,
you cannot just write “Shutdown Computer”, that is not a valid
syntax. The correct syntax would be “shutdown –s”, but we’ll get
into that later on.
-7-
A list of all the commands will appear. Since this is only a basic
Batch File tutorial, I won’t explain all of them but most of them I
will.
CD
This command takes you to another directory. When you first open
the command prompt, you are located in the directory:
C:\WINDOWS\system32
cd\
cd Program Files
cd Internet Explorer
It works the same; they both take you to the Internet Explorer
directory.
cd..
-8-
CLS
This command clears the screen. Not the Windows screen, the
command prompt screen.
Every command that you have written or scripted will be cleared.
This command is useful for a sequence of tasks on a batch file. For
example: if you want to go to a directory. After going to the
directory, everything you typed is written. You would then type
CLS and then hit enter. Everything will be cleared.
However, CLS is almost never needed when you have a batch file
in “echo off”. I’ll explain that later on.
MD
This command creates a new directory in the directory you are
currently in. If you are in C:\Documents and Settings\ you could
type:
MD New Folder or
MD Any Name
-9-
TITLE
This command lets you change the title of the Command Prompt.
The syntax of this command would be:
COLOR
This command lets you change the color of the Command Prompt.
If you type “color help” in the command prompt, all the color
codes will appear. If I wanted to change the background to black
and the foreground to green, I would type:
COLOR 0a
- 10 -
It would end up looking something like this.
ECHO ON or OFF
ECHO ON makes every command that you type visible. If you type
@ECHO OFF, every command and syntax you type is not going to
be shown, only the result will be displayed. For secret Batch Files I
would recommend you use @ECHO OFF to prevent people from
seeing the commands that are being executed in the Batch File.
ECHO
This command lets you display messages. Not popup messages,
messages printed onto the Command Prompt. The correct syntax
for this would be:
ECHO <message here>
An example would be:
ECHO Acid Crasher X was here...
Also if you type ECHO. (with a dot after “ECHO”) you skip a line
leaving it blank.
- 11 -
COPY
- 12 -
TIME and DATE
These commands are used when you want to set a new time or date
on the computer. By typing only TIME on the command prompt
you can see the current time. By typing only DATE you can see the
current date.
The syntax for TIME would be:
TIME 00:00:00.00
(hh:mm:ss.ml)
DATE <mm-dd-yy>
RMDIR
This is a useful command for removing directories. It only works
when the directory is empty. The appropriate syntax would be:
cd\
cd Directory
cd Next Directory
cd Next Directory
RMDIR Directory
If you don’t want to stay in that directory, you can use this other
syntax:
RMDIR “C:\directory\directory\directory”
- 13 -
DEL
This is another very useful command. It is as important as COPY
but this command is a lot more dangerous to mess with.
Warning: When using this command, the deleted file(s) are not sent
to the recycle bin, they are removed from the computer so do not
interfere with system files.
DEL file.extension
But, like any other command you must first go to the directory the
file is in or you must type the path in the syntax. Example:
DEL “C:\Directory\Directory\file.extension”
ERASE
DEL “file.extension”
…you say:
ERASE “file.extension”
- 14 -