Goals
In this session you will:
- make a simple batch file
- learn the batch-specific commands
- demonstrate your oveall understanding: make a functioning dos-based menuing system
Review
Pop Quiz activity thingy
- go to the root directory of your floppy
- make a directory called "pq2"
- change to the pq2 directory
- look at a directory listing of the root directory of the C: drive. It will be several pages long and will scroll off your screen.
- Now look at that c:\ listing again, but pipe the output to the more command so you can see it one page at a time.
- do the dir listing of c:\ again, and send the output to a file called "dirc".
- do a dir listing of the root directory on your A: drive, and send the output to a file called "dira".
- do a dir listing of your current directory: if you see the files "dira" and "dirc" listed, you've probably done this activity correctly. type or edit the contents of both so you can be sure. The output of the dir listings above should be found in these files.
Batch files
Make a simple .BATch file, similar to your autoexec.bat. Remember that each line will be interpreted as a command, and that you can use REM to make comments
REMarks
Use remarks heavily to document your code. A typical header
rem mybatch.bat
rem a way to automate payroll
rem jason carr, mouse@mousetrap.net, 10 Jan 1988
Modern DOS versions allow you to use :: instead of REM, whith some increase in efficiency.
echo
A batch displays each command line before running it, by default. You can turn off this behaviour by
useing the echo offcommand. Want to suppress that statement? Preface it with a @: @echo off
You may use echo. or echo: to display a blank line.
call
You can jump off to another batch file by merely mentioning it on the
command line.
Or you can run a batch file and return to the calling batch file using the call command: call extra.bat
passing an arg to a batch file
The args are referred to as %1-$9
%0 is the actual batch file name. You can have more than 9 args, just SHIFT them down...
for
for %%a in (1 2 3 4) do echo %%a
if exist
if not exist foo.bat copy \bin\foo.bat
if
if "%1" == "ed" goto ed
errorlevels
Frequently when programs terminate they leave a errorlevel> (aka exit level). Following the programs documentation, you can track the final condition of the program.
if errorlevel 6 goto blahhh
Note you must test in inverse order, 6,5,4,3,2,1, etc.
goto:label
Use :label to mark a certain position in a file, and use goto to branch off to that location...
errorlevels
An errorlevel is a way an exiting program can leave a clue about how and why it existed. 0 might mean normal exit, 1 might mean "out of disk space", 2 might mean "critical files were missing", etc.
choice
Choice is a batchfile command that allows the user to select a predefined choice, after which the program exits with an errorlevel. You can control the batchfiles flow depending on the value of this errorlevel.
You'll have to read the /? page to see the directions for this one; it's complex.
killing a runaway batch
<CTRL-c>
Conclusion
Your understanding of the boot procedure, file and directory handling, editing, redirection
and batch files will greathly enhance your future PC experiences. I hope you'lllook back on this
time fondly!
Further reading