The magazine of the Melbourne PC User Group

Tutorial: LHA, .BAT Files and Pipes
Peter Freeman

Did you know that you can use multiple file names on an LHA command line to archive many different files into the same archive in the one operation?

You can even specify different paths. It works across network boundaries too.

Did you know about the /B option in the DIR command? Everyone knows about DIR /W or DIR /P but what is the result of DIR /B. Try it.

And what about the /S option?

They only appear in DOS 5 or higher but they can make life very easy indeed.

Ever wanted to archive every file on your system that had a .DOC or a .BAT extension, but you didn't want to chase it through every directory in which it might appear?

Here we are going to look at automated ways of making the computer hunt down nominated files, and invoke LHA to archive all the nominated files (including their path names) into a monster backup file.

Why use an archiver to perform backups? There are a few reasons
  • The compression is generally tighter
  • You can nominate the files you want included in more creative ways than with BACKUP
  • There are excellent shell programs to take the drudgery out of managing your archivers.
  • Archivers don't have the same reputation for falling over than does BACKUP
The exercise

To create an archive containing all the .BAT files on the hard disk, wherever they may be.

To do this we will search the hard disk for all .BAT files, create a list of them, pass that list to our archiver and create a backup of them complete with extended path names so they can be restored to the directories from where they came.

If you have not yet amassed a collection of BAT files you may substitute any other extension for the purposes of this exercise. INI or DOC will do.

The file names

LHA.EXE is located in subdirectory C:\UTILS\SHEZ. You should substitute the path name for the directory in which you keep your LHA.EXE. If LHA is on your path, then you don't need to tell DOS where it is by specifying the path name. Typing LHA will suffice. Of course you will need LHA.EXE on your computer somewhere to participate in these exercises.

BATBAK.LZH is the file we will create, containg all the batch files we can find. It is to reside on your A: drive.

What you do next

With a copy of this article to hand, sit in front of your computer. Put your machine at the DOS prompt in the root directory of C: drive.

If you don't yet know how to do that, come back to this article after you have mastered a few more elements of command line computing. Or if you are feeling brave, type:
C:
CD\

You will now be at the root directory. Next type in

DIR   (and press Enter of course)

Did you see the directory listing without sizes, times, dates or other usual palaver? Did the listing stop after displaying one page only??

If it did stop, then you have an environmental variable called DIRCMD set. This will bring you to grief in the later parts of this exercise. You can confirm whether or not DIRCMD is affecting how your DIR command behaves by typing

SET (and pressing Enter)

You will see some text displayed. Is there a line beginning "DIRCMD= " ? If so, remove it by typing

SET DIRCMD  (and press Enter)

Don't worry about the effect of this; the original settings will be restored next time you reboot.

Now try

DIR /B/S

and just so you can see more clearly what you get, add the option /P to the command and try it again. You may see the directories themselves listed. It will look something like Figure 1 (without the comments).


Figure 1.

Next try this sequence:

DIR *.BAT /B/S

You will see all your batch files scrolling before your eyes, complete with path names. The next stage of the exercise will feed this list of full path name files to the archiver....

Now type the LHA command, preceded by its path name. On my machine it is

C:\UTILS\SHEZ\LHA.EXE

You will need to put in your own path name to suit your filing structure. If LHA is on your PATH then DOS can find it without needing a path name to show where it is, so just LHA will suffice.

I am using the convention that lowercase commands will need the substitution of the structure that is appropriate to your computer, whereas uppercase commands are applicable on all machines.

Did you see the help screen? The LHA help screen is a boon to a regular user of the archiver, but it can really put the frighteners on novices. It is in Figure 2.


Figure 2.

Without going into all the options in detail, the command we are going to use is

LHA A /X1 A:\BATBAK.LZH
  • LHA is the name of the executable
  • A is the ADD command - it adds files into an archive
  • /X1 is an advanced option that makes LHA store the full path name or directory structure so that when you de-archive the restored file can be put back into the same directory that it came from. The default is not to store path name information. (Don't ask why - other archivers use the opposite convention - they do store the path name by default)
  • A:\BATBAK or A:BATBAK.LZH is the name of the compressed file that is to contain all our batch files from all over the disk.
Normally there would be more to come on the command line, namely the names of the files we were about to compress. For example, (all on one line)
LHA A /X1 A:\BATBAK C:\CONFIG.SYS C:\WIN\WIN.INI C:\WP4\WP4.CFG

would store the files CONFIG.SYS, WIN.INI AND WP4.CFG, along with their path names, into an archive on the A: drive. If we left out the /X1 option then the same files would be archived, but without recording in which directories they were found.

Now comes the heart of the matter: we are about to join up the DIR /B command, and the LHA command. This is done with a DOS pipe. If you ever wondered what the symbol | is about now is the time you will find out.

It is not for drawing the vertical lines in a poor man's ASCII box. It is a potent command, called the DOS pipe, and it sends the output from one command, not to the screen, but into a following command so that the results of the first operation become the input for the second or subsequent operations.

Use of the DOS pipe changes your use of your computer from Command Line Computing to Assembly Line Computing, because the results of one operation are passed to another "workplace" for further processing.

Type the following (all on one line and adjusting the instructions in small type to those that are correct for your machine)

DIR *.BAT /B/S | C:\UTILS\SHEZ\LHA A /X1 A:BATBAK

Did you remember to put a floppy disk in drive A: ?? Let's see if all went well. We will now look at the file list of the archive.

Type:

C:\UTILS\SHEZ\LHA V A:\BATBAK

Do you see a scrolling list of files, complete with directory names, being read from the A: drive?

You must use the V command and not the more familiar L command. The L command will not show you the directory structure. Naturally, if an archive has been created without recording directory structure then the two ways of looking at the file list will be equivalent and either will do, but here we are interested in seeing the full structure that we have just recorded.

That completes our practical exercise in using LHA.

Were you to use it to implement a real backup you would use the command (all on one line)

C:\UTILS\SHEZ\LHA X A:\BATBAK YOHOHO.BAT YIPPEE.BAT

to restore YOHOHO.BAT and YIPPEE.BAT to their rightful directories. You may even need to add the /C option somewhere on the command line to force an overwrite if YIPPEE.BAT or YOHOHO.BAT exists on the hard drive.

So there you have it. I have endeavoured to present an easily accomplished exercise in command line computing to show both its power, its usefulness, and to dispel a few of the demons that surround the DOS prompt. Let me know how the exercise worked for you via a message to me, in area 11 on our BBS.

Now a note for not-so-novice users

The syntax we have used in this tutorial exercise is not the only way to achieve the result we have. One of the benefits of modern archivers is that there are usually a number of ways to achieve the same results. For example, the Bare directory listing (DIR /B) could have been imported into a database, and sophisticated search criteria used to derive a subset of the files which is in turn exported to a text file. That text file could then be passed to LHA to process its contents.

Using the techniques described here it is possible to construct a simple batch file that examines your hard disk for all .INI, .DOC,.TXT, .CFG and similar files, and creates full backups of each type of extension into its own archive file.

Reprinted from the April 1995 issue of PC Update, the magazine of Melbourne PC User Group, Australia

[About Melbourne PC User Group]