Batch Programming
DOS Batch Programming
Batch files are text files with commands that are interpreted and executed under DOS.
This gives Batch files the ability to affect the entire computer. Batch files can be
identified by their file name extension of "dot BAT". Some e-mail servers won't even
permit them to be sent as attachments, as "dot EXE" files prohibited. Batch files are
powerful enough execute malicious code if opened by a user. For those of us with no
malicious intent, this is a warning that BAT files should be used carefully. If I were
to make a mistake in my script, or make a typo, ( however unlikely ;-), it could provide
some pretty unpredictable results. Don't be discouraged, just be careful.
There is a wealth of information concerning BAT files on the web. Just be careful with
the information because mistakes can be made by well meaning authors. This can even be
said for this tutorial. I like to think myself easily understood, but there is always
the possibility that I might be mis-understood. Or even worse; (heaven forbid) there is
the remote possibility that I could even make a mistake. Don't let this stop you. Simply
satisfy yourself that you know what your doing and keep some kind of UNDO method in mind.
Windows 2000 gives help on DOS commands with:
"Start", then "Help", then "Reference", then "MS-DOS Commands"
Now that the basic overview has been presented, and suitable warning concerning the
power of BATCH programming, I will present some information that you will find useful.
I have a Windows XP station with useful information available using my Window's
Operating system.
Click on "START", then "HELP AND SUPPORT", then "tools".
In the -Pick a Task- section there are three useful links at the bottom of the list.
These are a few of the commands that may be used with batch (or script) programs.
A----Arp , Assoc , At , Atmadm , Attrib
B----Batch files , Bootcfg , Break
C----Cacls , Call , Change , Chcp , Chdir , Chkdsk , Chkntfs , Cipher , Cls , Cmd , Cmstp , Color , Command shell overview , Comp , Compact , Convert , Copy , Cprofile , CScript overview
D----Date , Defrag , Del , Dir , Diskcomp , Diskcopy , DiskPart , Doskey , Driverquery
E----Echo , Endlocal , Eventcreate , Eventquery , Eventtriggers , Evntcmd , Exit , Expand
F----Fc , Filter commands , Find , Findstr , Finger , Flattemp , For , Format , Fsutil , Ftp , Ftp subcommands , Ftype
G----Getmac , Goto , Gpresult, Gpupdate , Graftabl
H----Help , Helpctr , Hostname
I----If , Ipconfig , Ipseccmd , Ipxroute , Irftp
L----Label , Lodctr , Logman , Lpq , Lpr
M----Macfile , Mkdir (md) , Mmc , Mode , More , Mountvol , Move , MS-DOS , subsystem configuration commands , Msiexec , Msinfo32
N----Nbtstat , Net services overview, Net services commands , Netsh command , verview , Netsh commands for AAAA , Netsh commands for DHCP , Netsh , diagnostic (diag) commands , Netsh commands for Interface IP , Netsh commands for RAS , Netsh commands for Routing , Netsh commands for WINS , Netstat , Nslookup , Nslookup subcommands , Ntbackup , Ntcmdprompt , Ntsd
O----Openfiles
P----Pagefileconfig , Path , Pathping , Pause , Pbadmin , Pentnt , Perfmon , Ping , Popd , Print , Prncnfg , Prndrvr , Prnjobs , Prnmngr , Prnport , Prnqctl , Prompt , Pushd
Q----Query
R----Rasdial , Rcp , Recover , Redirection operators, Reg , Regsvr32 , Relog , Rem , Rename , Replace , Reset session, Rexec , Rmdir , Route , Rsh , Rsm , Runas
S----Sc , Schtasks , Secedit , Set , Setlocal , Shift , Shutdown , Sort , Start , Subst , Systeminfo , System File Checker (sfc)
T----Taskkill , Tasklist , Tcmsetup , TCP/IP utilities and services, Telnet commands , Terminal Services commands , Tftp , Time , Title , Tracerpt , Tracert , Tree , Type , Typeperf
U----Unlodctr
V----Ver , Verify , Vol , Vssadmin
W--W32tm , Winnt , Winnt32 , WMIC overview
X--Xcopy
There are also some useful links in the"Useful Links and Adds" on our tutorial's home
page.
I have some fun examples here for you to play around with as well as some
suggestions for some Batch files of your own. I intend to use only passive commands
that only return information and create files that may be easily deleted after the
demonstration of the batch file.
In this exercise, I will create a simple bat file and a text file on the desktop.
They will have simple names that are easily identified and easy to locate for
deleting when I am done with the demonstration.
The echo command displays the text in the command argument. Lets post a message.
1- Create a file on the desktop and remane it to temp.bat
2- type a message, such as: "echo This is my first bat file" and close the file.
3- execute the bat file by double clicking on it.
the text in the bat file will be shown in color
echo This is my first Bat File
Clearly something happened. Something was posted on the desktop, but it was't there
long enough to see. We need an instruction that waits for us to permit the returned
data to be seen. This is the function of the PAUSE instruction. Add it and execute
it again.
echo This is my first Bat File
pause
The pause permits us to see the text until we press a key. Still its not exactly
what we want. All we want is the text we wish to echo, not the entire command
prompt and the entire command string as well. We can disable the command string
from being echoed by starting the batch string with ECHO OFF. Add it.
echo off
echo This is my first Bat File
pause
Great, only the first command is echoed because its input before the actual echo
command is executed. All commands following the echo off command are supressed.
So, lets clear the screen with a clr command the clears the screen.
echo off
cls
echo This is my first Bat File
pause
Now its the way we want.
This is so common with batch programs that a special form of the echo off is used.
The echo off and clr are replaced with the @ ECHO OFF command.
If it doesn't work with older DOS, then go back to the echo off and clr method.
@ echo off
echo This is my first Bat File
pause
That was rather long winded, I know, but it really needed to be explained. Now we will
see the outputs, but not the inputs to the command interpreter. Any time we want to see
the inputs again, we only need to turn echo back on with and ECHO ON command. We are
ready for the fun part now. We will get some returns that we can use.
We could use a little help here. How do we get it?
@ echo off
echo Send me some help !!!
help
pause
Perhaps its time to summarize.
We have written a text file with a bunch of instruction.
Double click on it and it becomes opened with NotePad or whatever program has
been associated with files with the dot txt extension. Nothing new.
Then we change the extension to dot bat and it becomes the input to a
command line interpreter named CMD.EXE or perhaps command.com .
The Intepreter will interpret the command string in the batch file and execute
these commands using our DOS (Disk Operating System) . Very powerful indeed,
considering that DOS is the set of commands that essentially permit the Windows
Operating System to function. All that is necessary for us is to learn these
commands and we can do strange and wonderful things with our computer.
It has probably already occurred to you that is would be easier to input these
commands from the keyboard rather than inputting them from a batch file. True,
for the examples that I have given, but when a set of instructions become very
large, or require IF conditions to do different things, it is much nicer
to be able to type it all into a text file once, than to find it necessary to
repeat the typing task repeatedly over and over again. The use of the DOS's command
line interpreter is covered under our DOS section more thoroughly than here. It is
highly recommended that a good understanding of the DOS commands be learned prior
to attempting very many batch files. Always consider a way back from any new batch
stream. You may find yourself wanting to undo something that you may inadvertantly
have done. If that sounds like the voice of experience; well..., it is.
Command Line Instructions
Where to find the Instruction set for the DOS Command Line Interpreter.