Skip to content
August 21, 2008 / technocrawl

Batch File Programming

A batch file is an unformatted text file that contains one or more commands and has a .bat or .cmd file name extension. When you type the file name at the command prompt, Cmd.exe runs the commands sequentially as they appear in the file.

You can include any command in a batch file. Certain commands, such as for, goto, and if, enable you to do conditional processing of the commands in the batch file. For example, the if command carries out a command based on the results of a condition. Other commands allow you to control input and output and call other batch files.

The standard error codes that most applications return are 0 if no error occurred and 1 (or higher value) if an error occurred. Please refer to your application help documentation to determine the meaning of specific error codes.

Whatever the follows this command will be displayed as it is, which is just like printf statement in ‘C’ language.
C:\>echo Hello world
Hello world


This command is used to create a directory or folder on the specified location. you can also use mk command, which does the same operation.

C:\>mkdir sample folder
This will create a folder named “sample folder” in the C drive.


This command is used to delete a directory or folder on the specified can also use rm command, which does the same operation.

C:\>mkdir sample folder


This command is used to copy a file from one location to another location.
Copy location\sourcefile location\destinfile.

C:\>copy C:\batch f:\batch

This command is used to delete a specific file.

C:\>del passwords.txt

Graphically displays the folder structure of a drive or path


Sets the window title for the command prompt window.
C:\>title Windows Terminal

this will sets the title of the command prompt to Windows Terminal

Sets the default console foreground and background colors.
Color background color foreground color.
Color 0a

0 = Black 8 = Gray
1 = Blue 9 = Light Blue
2 = Green A = Light Green
3 = Aqua B = Light Aqua
4 = Red C = Light Red
5 = Purple D = Light Purple
6 = Yellow E = Light Yellow
7 = White F = Bright White

You can choose any of the color from the given table.
color 0a will set the background color as black and foreground color as green.

C:\>prompt /?
Changes the cmd.exe command prompt.
Prompt can be made up of normal characters and the following special codes:

$A & (Ampersand)
$B | (pipe)
$C ( (Left parenthesis)
$D Current date
$E Escape code (ASCII code 27)
$F ) (Right parenthesis)
$G > (greater-than sign)
$H Backspace (erases previous character)
$L prompt CmdH4ck5 $$
CmdH4ck5 $

Instead of C:\>, now you will get CmdH4ck5 $ displayed at the beginning of each line.

Clears the screen.


Opens notepad application

Notepad note1.txt
This will opens a new notepad file with name note1.txt.

This is used to call another batch program or internal command line executables to your batch file,
and if the batch file doesn’t exists then it will display you a error message.

call ping

This piece of code will call the windows internal executable “ping”.

Goto Label:

This can be used for unconditional branching. here is a sample snippet that clearly demonstrate how it works.

@echo off
rem clear is a label
goto clear

This piece of code when executed will show he OS structure, and once done, the control is transfered to the label clear by using the goto statement, and as a result it will clears the screen.


start is used to execute windows internal executable by calling them.
here are the switches for the start statements.
Starts a separate window to run a specified program or command.

START [“title”] [/Dpath] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
[/WAIT] [/B] [command/program]

title” Title to display in window title bar.
path – Starting directory
B – Start application without creating a new window. The
application has ^C handling ignored. Unless the application
enables ^C processing, ^Break is the only way to interrupt
the application
I – The new environment will be the original environment passed
to the cmd.exe and not the current environment.
MIN – Start window minimized
MAX – Start window maximized
SEPARATE – Start 16-bit Windows program in separate memory space
SHARED – Start 16-bit Windows program in shared memory space
LOW – Start application in the IDLE priority class
NORMAL – Start application in the NORMAL priority class
HIGH – Start application in the HIGH priority class
REALTIME – Start application in the REALTIME priority class
ABOVENORMAL -Start application in the ABOVENORMAL priority class
BELOWNORMAL – Start application in the BELOWNORMAL priority class
WAIT – Start application and wait for it to terminate
If it is an internal cmd command or a batch file then
the command processor is run with the /K switch to cmd.exe.
This means that the window will remain after the command
has been run.

If it is not an internal cmd command or batch file then
it is a program and will run as either a windowed application
or a console application.

parameters These are the parameters passed to the command/program

If Command Extensions are enabled, external command invocation
through the command line or the START command changes as follows:

non-executable files may be invoked through their file association just
by typing the name of the file as a command. (e.g. WORD.DOC would
launch the application associated with the .DOC file extension).
See the ASSOC and FTYPE commands for how to create these
associations from within a command script.

When executing an application that is a 32-bit GUI application, CMD.EXE
does not wait for the application to terminate before returning to
the command prompt. This new behavior does NOT occur if executing
within a command script.

When executing a command line whose first token is the string “CMD ”
without an extension or path qualifier, then “CMD” is replaced with
the value of the COMSPEC variable. This prevents picking up CMD.EXE
from the current directory.

When executing a command line whose first token does NOT contain an
extension, then CMD.EXE uses the value of the PATHEXT
environment variable to determine which extensions to look for
and in what order. The default value for the PATHEXT variable


Notice the syntax is the same as the PATH variable, with
semicolons separating the different elements.

When searching for an executable, if there is no match on any extension,
then looks to see if the name matches a directory name. If it does, the
START command launches the Explorer on that path. If done from the
command line, it is the equivalent to doing a CD /D to that path.

start /max notepad
this will opens a notepad file in a maximized mode.

Exit the DOS mode or closes the current Command prompt window.


Pause statement makes will pause the batch file from executing further and waits for users input, and displays “Press any key to continue . . .”, if user presses any key, then it will start executing the rest of the file.


rem is used for commenting. which ever that follows the rem statement will be ignored in the same line.

Changes the position of replaceable parameters in a batch file.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: