The magazine of the Melbourne PC User Group

Roaming the CDs
Peter Selig
pfselig@melbpc.org.au

This article describes some small Dos batch files, developed to ease he process of moving around d1isks and directories. These enable the user to "save" the current directory and drive and, later, return to them from any other directory/drive.

Readers of the June 94 PC Update may recall an article by Geoffrey Campbell, in which two Turbo Pascal programs accomplish the same tasks (that article inspired me to develop these batch files). One advantage of batch files is that everyone with an IBM-compatible PC can use them - you do not need Turbo Pascal (or any high-level programming language). I hope some of the techniques used will interest both people who use DOS batch files, and anyone interested in exploring what can be done using DOS, without resorting to assembly language.

Initial Preparation

The batch files assume that all the files used in this process will be stored in the C:\BATCH directory. This directory must be included in the DOS PATH. If you use a different directory/drive, you will need to change the references to C:\BATCH in all the files to match the directory/drive you use (also, change the directory/drive shown in the instructions for creating the CD.CMD file).

As an example of the changes needed, suppose you keep your batch programs in the C:\BATS directory. Consider the file SAVEDIR.BAT; a line that reads:
CD c:\batch\dirstor.txt
would change to:
CD c:\bats\dirstor.txt

One final point to check-the DOS program FIND.EXE (it might be FIND.COM for some versions of DOS) must be present on your hard disk; it is used by SAVEDRIV.

Creating the Files

The two main programs, SAVEDIR and SWAPBACK, require several other programs and files in order to work. Some of these programs must be created by the user; others are automatically created when the main batch files are run.
:SAVEDIR.BAT
REM Saves the current drive/directory
REM to a file for later restoration
@ECHO off
CD > c:\batch\dirstor.txt
SAVEDRIV
:SAVEDRIV.BAT
REM Stores current drive to a file
@ECHO off
VOL I FIND "Volume in" > c:\batch\volu.bat
VOLU
:VOLUME.BAT
REM Creates file "DRIVSTOR.TXT", containing the current drive
@ECHO off
ECHO %3 > c:\batch\drivstor.txt
DEL c:\batch\volu.bat
:SWAPBACK.BAT
REM Returns user to previous drive & directory
@ECHO off
COPY c:\batch\cd.cmd+c:\batch\dirstor.txt c:\batch\goto dir.txt>NUL
COPY c:\batch\drivstor.txt+c:\batch\goto_dir.txt c:\batch\goback.bat>NUL
DEL c:\batch\dirstor.txt
DEL c:\batch\drivstor.txt
DEL c:\batch\goto_dir.txt
GOBACK

For people creating the files themselves, the files to be created are:
SAVEDIR.BAT 
SWAPBACK.BAT 
SAVEDRIV.BAT 
VOLUME.BAT 
CD.CMD (see instructions following to create this file)

You should create the first four files, as they appear in the listings - if necessary, amend the directory/drive references.

The files which are automatically created are: DIRSTOR.TXT, VOLUBAT, DRIVSTOR.TXT, GOTO DIRTXT and GOBACKBAT.

To create the file CD.CMD, in the C:\BATCH DIRECTORY, type the following at the DOS command prompt:

COPY CON C:\BATCH\CD.CMD
CD ^Z

On the second line, a single space follows the letters CD. ^Z indicates that Ctrl+Z is pressed following the space. Press the Enter key following Ctrl+Z. DOS should display the message: "1 file(s) copied" confirming that the file has been created.

Running

To use these files, first run SAVEDIR to save the current directory/drive. This is done by typing SAVEDIR at the DOS command prompt. Then, change to another directory and/or drive and do whatever you wish. Then type SWAPBACK at the DOS command prompt, which runs the SWAPBACK batch file, to change back to your initial directory/drive.

Explanation

This section focuses on a couple of unusual techniques used, whose working may not be obvious at first sight. Variables like %0, %1, %2, and so on, refer to the words on the command line. %0 is the first word on the command line, which is always the command. %1 is the next word, %2 is the following word, etc. SAVEDRIV BAT uses the VOL command. The output of this command, "filtered" through the FIND command, is a line having one of the two forms shown below ("A" drive assumed):

Volume in drive A is NAME
or
Volume in drive A has no label

This line is stored in the VOLU.BAT batch file, and is then run as a command. When run as a command, the vOLUME.BAT file can "pick out" the drive letter from this line, which enables the switching of drives in the SWAPBACK file.

SWAPBACK.BAT constructs a command of the form: CD directory. This is done by concatenating a "dummy" file, holding the text CD, with a file containing the directory, "saved" by the SAVEDIR program.

What Next?

If these techniques arouse your imagination, you could adapt them for other purposes. For example, use the DOS DATE command in a batch file to create a simple, personalised, reminder system:

"Today is Tuesday - Weekly Accounts Report due"
or
"It is Friday - Arrange Sunday lunch with David's parents".

Include a line in your AUTOEXEC.BAT file that runs this batch program, and you have an automated reminder system for yourself. Of course, this would not be as fancy, or powerful, as the commercial diary programs available, but it will be your own creation. Also, the do-it-yourself approach costs you nothing - except time. The sense of mastery, and understanding you gain makes it well worthwhile. 

Bibliography 
Supercharging MS DOS; Van Wolverton, Microsoft Press, 1989. 
Expert Advisor: DOS; Jonathon Kamin, Addison-Wesley, 1989. 

Reprinted from the September 1994 issue of PC Update, the magazine of Melbourne PC User Group, Australia

 

[About Melbourne PC User Group]