I am the king
olaf
   C    C++    Java    JavaScript    Perl    Lisp   
   DOS    Unix    Electronics    PC Build    Windows    Boolean Logic    Networking Concepts               

DOS, Page 3: Batch File Programming

Disk Operating System (or Dumb Operating System)

Previous Pages:
Introduction to using DOS
Using DOS in IBM OS/2
DOS commands
Command Index
Tips for using DOS
DOS Network Utilities
DOS PC/Hardware Utilities
How to make and use bootable floppy disks

This Page:
Creating and using batch files in DOS
Batch file utilities and commands
The AUTOEXEC.BAT file

Next Pages:
Parameters in batch files
DOS in Windows NT






Batch Files

What are batch files? Batch files are not programs, pre se, they are lists of command line instructions that are batched together in one file. For the most part, you could manually type in the lines of a batch file and get the same results, but batch files make this work easy. Batch files do not contain "compiled" code like C++ so they can be opened, copied and edited. They are usually used for simple routines and low-level machine instruction, but they can be very powerful. If you look in your C:\ or C:\WINDOWS folder you will see a multitude of .BAT, .SYS, .CFG, .INF and other types. These are all kinds of batch files. This may shock you, but while most applications are writen in Basic or C++ they sit on a mountain of batch files. Batch files are the backbone of the Windows operating system, delete them and you've effectively disabled the OS. There is a reason for this. The system batch files on each computer are unique the that computer and change each time a program is loaded. The operating system must have access to these files and be able to add and delete instructions from them.


Creating Batch files


This is a little batch file I wrote that I use every day. It deletes the cookies that get dumped to my hard drive every time I go online. I could set my browser preferences not to accept cookies, but sometimes cookies are useful. Some CGI pages are unusable with cookies, sometimes when you enter a password for a Website, the site uses a cookie to remember your password. I just do not need hundreds of cookie files taking up space after I close my browser. With this batch file, all I have to do is double-click it and it deletes my cookies. Feel free to cut and paste this code to your Notepad or Wordpad. Save it as cookiekill.bat on your Desktop.


cls


REM *******************************************
Rem ** Cookie Kill Program **
REM *******************************************

deltree /y c:\windows\cookies\*.*
deltree /y c:\windows\tempor~1\*.*
pause
cls
REM Cookies deleted!



:end


What does the batch file do? The first line has the command cls. cls clears the screen window of any previous data. The next three lines start with REM for "remark." Lines begining with REM do not contain commands, but instructions or messages that will be displayed for the user. The next two lines begin with the command deltree, deltree not only deletes files but directories and sub-directories. In this case the file is deleting the directory cookies and all the files inside. This directory is automatically rebuilt. The deltree has been passed the parameter /y, this informs the process to answer "YES" to any confirmation questions. Sometimes you type the DEL or one of its cousins, the system will ask "Are sure you want to do this?" setting /y answers these prompts without interupting the process. The pause command halts the process temporarily and shows the users a list of all the files being deleted. cls clears the screen again, another REM line tells the user that the files are deleted. The last line contains only :end and returns the process to the command prompt. This version was created to show the user everything that is taking place in the process. The version bellow does the same thing without showing the user any details.

cls
@echo off

deltree /y c:\windows\cookies\*.*
deltree /y c:\windows\tempor~1\*.*

cls


Without REM lines there are no comments. The @echo off command keeps the process from being "echoed" in the DOS window, and without the pause and :end lines, the process runs and exits without prompting the user. In a process this small it is okay to have it be invisible to the user. With more a complex process, more visual feedback is needed. In computing there is fine line between too much and too little information. When in doubt give the user the oportunity to see what is going on.

This version is a little more thurough, deletes alot of junk
cls
@ECHO OFF ECHO. *******************************************
ECHO. ** Clean Cookies and Temp Files **
ECHO. *******************************************
deltree /y c:\windows\cookies\*.*
deltree /y c:\windows\tempor~1\*.*
deltree /y c:\progra~1\Netscape\Users\default\Cache\*.jpg
deltree /y c:\progra~1\Netscape\Users\default\Cache\*.gif
deltree /y c:\progra~1\Netscape\Users\default\Cache\*.htm
deltree /y c:\progra~1\Netscape\Users\default\archive\*.htm
deltree /y c:\progra~1\Netscape\Users\default\archive\*.gif
deltree /y c:\progra~1\Netscape\Users\default\archive\*.jpg
deltree /y c:\windows\temp\*.*
deltree /y c:\temp\*.*
deltree /y c:\windows\Recent\*.*
deltree /y c:\recycled\*.*
@ECHO OFF
cls


One thing I do quite often is erase old floppy disks. I might have a stack of them and I don't care what's on them, but I want all the files gone including potential virii(everyone says "viruses" but "virii" is the proper term. Snob!). But I get tired of opening a DOS prompt and typing in the command to format the disk. So I wrote a one line batch file that does it for me. Save it as: "disk_wipe.bat"

format a: /u
Put a disk in the drive and double-click the .bat file icon.



Batch File Utilities and Commands*

*Note: Any valid DOS command may be placed in a batch file, these commands are for setting-up the structure and flow of a batch file.



CLS

Clears the screen

MSCDEX

Loads the CD-ROM software extensions(drivers), usually so an operating system can be then loaded from CD. See the AUTOEXEC.BAT section for special instructions concerning CD ROM installation.

CALL

Calls another batch file and then returns control to the first when done.
CALL C:\WINDOWS\NEW_BATCHFILE.BAT


LASTDRIVE

Sets the last drive in the system.

EXIT

Exits the command-line process when the batch file terminates
lastdrive=Q
EXIT


BREAK

When turned on, batch file will stop if the user presses < Ctrl >-< Break > when turned off, the script will continue until done.
BREAK=ON

BREAK=OFF


CHOICE

Allows user input. Default is Y or N.
You may make your own choice with the /C: switch. This batch file displays a menu of three options. Entering 1, 2 or 3 will display a different row of symbols. Take note that the IF ERRORLEVEL statements must be listed in the reverse order of the selection.
@ECHO OFF
ECHO 1 - Stars
ECHO 2 - Dollar Signs
ECHO 3 - Crosses


CHOICE /C:123

IF errorlevel 3 goto CRS
IF errorlevel 2 goto DLR
IF errorlevel 1 goto STR

:STR
ECHO *******************
ECHO.
PAUSE
CLS
EXIT

:DLR
ECHO $$$$$$$$$$$$$$$$$$$$
ECHO.
PAUSE
CLS
EXIT

:CRS
ECHO +++++++++++++++++++++
ECHO.
PAUSE
CLS
EXIT




FOR...IN...DO




GOTO

To go to a different section in a batch file. You may create different sections by preceding the name with a colon.
:SUBSECTION
Programmers may find this similar to funtions or sub-routines.

:FIRSTSECTION
REM This is the first section
GOTO :SUBSECTION

:SUBSECTION
REM This is the subsection
GOTO :END
:END

Looping with GOTO
:BEGIN
REM Endless loop, Help!!
GOTO :BEGIN

IF (IF NOT EXIST)

IF EXIST C:\tempfile.txt
DEL C:\tempfile.txt
IF NOT EXIST C:\tempfile.txt
COPY C:\WINDOWS\tempfile.txt C:\tempfile.txt



PAUSE

Pauses until the user hits a key.

This displays the familiar "Press any key to continue..." message.



REM

Allows a remark to be inserted in the batch script.

REM DIR C:\WINDOWS Not run as a command
DIR C:\WINDOWS Run as a command



ECHO

Setting ECHO "on" will display the batch process to the screen, setting it to "off" will hide the batch process.
@ECHO OFF Commands are displayed
@ECHO ON Commands are displayed





The AUTOEXEC.BAT file

AUTOEXEC.BAT stands for automatic execution batch file, as in start-up automatically when the computer is turned on. Once a very important part of the operating system, it is being less used and is slowly disapearing from Windows. It is still powerful and useful.

Before the graphical user interface(GUI, "gooey") of Windows, turning on a PC would display an enegmatic C:\> and not much else. Most computer users used the same programs over-and-over, or only one program at all. DOS had a batch file which set certain system environments on boot-up. Because this was a batch file, it was possible to edit it and add a line to start-up the user's programs automatically.

When the first version of Windows was released users would turn their PCs on, and then type: WIN or WINDOWS at the prompt invoking the Windows interface. The next version of Windows added a line to the AUTOEXEC to start Windows right away. Exiting from Windows, brought one to the DOS prompt. This automatic invocation of Windows made a lot of people mad. Anyone who knew how to edit batch files would remove that line from the AUTOEXEC to keep Windows from controling the Computer. Most users do not even know that DOS is there now and have never seen it as Windows hides the any scrolling DOS script with their fluffy-cloud screen. At work I will often have to troubleshoot a PC by openning a DOS shell, the user's often panic, believing that I have broken their machine because the screen "turns black" (silly mortals).

Most current versions of Windows have a folder called "Start-up." Any program or shortcut to a program placed in this folder will start automatically when the computer is turned on. This is much easier for most users to handle than editing batch files.

Old versions of DOS had a AUTOEXEC that looked like this:

@echo off
prompt $p$g

All this really did way set the DOS prompt to ">"

Later versions looked like this:
cls
@echo off
path c:\dos;c:\windows
set temp=c:\temp
Lh mouse
Lh doskey
Lh mode LPT1 retry
This AUTOEXEC.BAT loads DOS & then Windows. Sets up a "temp" directory. Loads the mouse driver, sets DOSKEY as the default and sets the printer retry mode. "Lh" stands for Load High, as in high memory.

An AUTOEXEC.BAT from a Windows 3.11 Machine
@ECHO On
rem C:\WINDOWS\SMARTDRV.EXE
C:\WINDOWS\SMARTDRV.EXE 2038 512
PROMPT $p$g
PATH C:\DOS;C:\WINDOWS;C:\LWORKS;C:\EXPLORER.4LC
SET TEMP=C:\DOS
MODE LPT1:,,P >nul
C:\DOS\SHARE.EXE /F:150 /L:1500
C:\WINDOWS\mouse.COM /Y
cd windows
WIN



This version simply sets DOS to boot to Windows.

SET HOMEDRIVE=C:
SET HOMEPATH=\WINDOWS


Whenever a program is installed on a computer, the setup program or wizard will often edit the AUTOEXEC. Many developer studios will have to "set a path" so programs can be compiled or run from any folder. This AUTOEXEC is an example of that:
SET PATH=C:\FSC\PCOBOL32;C:\SPRY\BIN
SET PATH=C:\Cafe\BIN;C:\Cafe\JAVA\BIN;%PATH%
SET HOMEDRIVE=C:
SET HOMEPATH=\WINDOWS


This AUTOEXEC sets the path for COBOL and JAVA development BINs. This way, the computer knows where to look for associated files for COBOL and JAVA files if they are not located directly in a BIN folder.





Sets all the devices and boots to Windows.
When the "REM" tags are removed the device commands become visible.
@SET PATH=C:C:\PROGRA~1\MICROS~1\OFFICE;%PATH%
REM [Header]
@ECHO ON
REM [CD-ROM Drive]
REM MSCDEX.EXE /D:OEMCD001 /L:Z
REM [Display]
REM MODE CON: COLS=80 LINES=25
REM [Sound, MIDI, or Video Capture Card]
REM SOUNDTST.COM
REM [Mouse]
REM MOUSE.COM
REM [Miscellaneous]
REM FACTORY.COM

For loading Windows from a CD
@echo off
MSCDEX.EXE /D:OEMCD001 /L:D
d:
cd \win95
oemsetup /k "a:\drvcopy.inf"
For loading CDROM drivers
Removing the "REM" tags uncomments the commands and runs them.
REM MSCDEX.EXE /D:OEMCD001 /l:d
REM MOUSE.EXE



The Windows Installation Catch-22

You have a new computer with a unformated hard drive, or a drive with only DOS loaded. You want to load Windows from a CD, but you can't see the CD ROM from the DOS prompt. This is messy and can be screwed-up easily, luckily mistakes on this don't cause permanent damage. If you're lucky the CD ROM you have came with an installation disk(on floppy, of course). Putting this disk in and running the INSTALL.EXE or SETUP.EXE will install the drivers for you and alter the system files so you can load Windows from the CD ROM(Linux, by the way, has no problem with this!). If there is no INSTALL.EXE on the disk, you will have to edit lines in two files on your Windows 95 Boot/Install floppy disk. These files are: CONFIG.SYS and AUTOEXEC.BAT. Open these files for editing are look for lines that look like these:

REM DEVICE=CDROM.SYS /D:OEMCD001

And
REM C:\DOS\MSCDEX.EXE /D:OEMCD001

They may or may not be REMed out. You will need to change the "/D:OEMCD001" part of these lines to reflect the CD ROM that you have. For example if you have a Memorex it might be "/D:MSCD001". But be sure, check any manuals you might have lying around. If not, go to the manufacturer's website and down load the installation files. You will also need to figure out which drive letter it will be. If you only have on hard disk, it will be "D:" as in "/D:MSCD001," if you have two hard drives, or your drive is in several partitions, it might be "E:" or "F:". So then the line would be "/E:MSCD001" or "/F:MSCD001"

The Final line in CONFIG.SYS might be like this:
DEVICE=C:\WINDOWS\SBIDE.SYS /D:MSCD001 /V /P:170,15



What the heck is "Chicago"?

If you open enough system files in Windows, you will see the reoccurring use of the word "chicago" with seemingly no purpose. When Windows95 was in early development its secret name was "Chicago." Either people were to lazy to take the reference out or they left in out of a sense of nostalgia. Chicago shows up in just about every Windows .inf, .ini, .sys and .dll file. Just search your drive for files containing the word "chicago" and you'll get well over 100.

A Look at CONFIG.SYS

REM [Header]
FILES=20
BUFFERS=20
DOS=HIGH,UMB
REM [SCSI Controllers]
REM DEVICE=SCSI.SYS
REM [CD-ROM Drive]
REM DEVICE=CDROM.SYS /D:OEMCD001
REM [Display]
REM DEVICE=DISPLAY.SYS
REM [Sound, MIDI, or Video Capture Card]
REM DEVICE=SOUND.SYS
REM [Mouse]
REM DEVICE=MOUSE.SYS
REM ------------------
REM [Miscellaneous]
REM DEVICE=SMARTDRV.EXE


<-------Previous Page !!!!!!! Next Page------>


Can't Find it Here?
Networking Index
Free Compilers List
Programming Concepts Index
Contact Information
Home
Recomended Computer Books
What's New
Get LINUX!
Excellent Links