Don't Get Burned
Matchmaker ensures the work you put into recording CDs doesn't go to waste.
I've spent a lot of time recently burning CDs.
No, I'm not getting rid of my disco music collection--I've actually been making my own data CDs. The recording process is known as burning.
Burning a CD is a solemn task, expensive (about $10 to $15 for blank CDs) and slow (a couple of hours to burn and verify). And even after you've spent the time and money, you might not have a usable CD. Like many folks making CDs, I've built quite a collection of expensive drink coasters.
Even after my CD-writing software assures me everything has gone well, the thought of sending my new CD masters off to a duplicating plant makes me a bit nervous. How can I be sure the thousands of files I meant to place on the CD are really there, under the right names, with the correct contents? I don't want tens of thousands of copies to come out wrong. After spending hours manually testing my new CDs by comparing their contents to my original data on hard disk, I decided there had to be a better way. There is ... I automated the process.
My solution is a Visual Basic (VB) program named Matchmaker. The program compares two directory trees and makes sure their contents are identical. In my case, one directory is usually the root directory of a CD I've created. The other directory resides on my hard disk and contains the files copied to the CD. But Matchmaker isn't limited to checking CDs. It can verify the contents of any removable disk, or even compare two hard disks. You can also use the program to compare a directory tree to itself.
Matchmaker is a modified version of the WinMag Disk Scanner. we discussed last December. The program now sports a matched pair of drive and directory list boxes. You use these to select the two directory trees you want compared. In between there are buttons to start and pause the comparison, and exit the program.
Below the list boxes and buttons are three check boxes. These determine how picky the program should be before declaring a match. At a minimum, Matchmaker ensures each file in one tree also exists in the other. But you can ask it to compare the sizes of the two files, their date and time of creation, and even the files' contents.
To the right of the check boxes are statistics Matchmaker updates. Finally, you'll find three text boxes at the bottom of Matchmaker's window. Two display the names of the two files currently being compared, while the third shows any error messages Matchmaker needs to display.
So much for Matchmaker's outward appearance. Now let's take a look below the surface, and get to know the program better.
Backslash backlashMost of the time, a backslash character ("\") can't be part of a directory's name. That's because the backslash serves a special purpose in the Windows and DOS filenaming schemes; it separates one directory name from another within a pathname. But there's an exception to this rule: The name of a drive's root directory does contain a backslash. In fact, "\" is the root directory's entire name.
This causes problems for folks who write programs that scan directory trees. The problem appears when it's time to concatenate a directory's name and a filename, to form the full, absolute pathname of a file. Should you insert a backslash between the directory's name and filename? That depends on the directory.
If it's a root directory, you want to avoid an extra backslash before the filename. Insert it and you'll end up with a pathname like C:\\CONFIG.SYS. But you need a new backslash if the directory isn't a root directory. Leave it out and you'll get a pathname like C:\DIRFILE when what you wanted was C:\DIR\FILE.
Programmers have devised several ways to solve this problem. My favorite is to make sure that directory names stored within a program always end with a trailing backslash. If a directory name doesn't already end with a backslash, I add one.
VB's built-in Right function, which allows us to examine characters on the right-hand end of a string, makes this job easy. Here's how Matchmaker does it:
First, Matchmaker retrieves a standard DOS directory name from the dir1 directory list box and stores it in the variable Trunk1. At this point, the directory name in Trunk1 may or may not end with a backslash. In line 2, the Right function compares Trunk1's right-most character to a backslash. If no backslash is found, line 3 adds one. Matchmaker follows this same procedure each time it retrieves a directory name and stores it for later use.
Now, when Matchmaker needs to form a full pathname from a directory name and a filename, it can just concatenate the two components without worrying about backslashes. Line 1 below shows this technique in action:
The variable Dirname contains a directory name. The file list box filWork1 contains a list of filenames. To form a pathname consisting of Dirname's directory, and the ith filename stored in filWork1, all we need is this simple VB statement: P1 = Dirname + filWork1.List(i).
Name gameThe code above displays the heart of our Matchmaker. It's where the program constructs the names of the files it will compare. To understand how this works, let's leave the forest for a moment to study the trees.
Real trees, the ones that grow outside your house, have a lot in common. Each has roots, one trunk, several branches and lots of leaves. Normally, the roots are at the bottom, the trunk is in the middle, and the branches and leaves on top.
Directory trees--the ones that grow inside your hard disk--are like trees with directories and files instead of branches and leaves. They also have trunks, and one or more directories (beginning with a drive's root directory) that lead to the base of the tree.
On most hard disks, you can consider C:\Windows a trunk. It forms the base of a large tree containing several subdirectories and files. Within the tree you'll find the directory C:\Windows\System, and the files C:\Windows\System\USER.EXE and C:\Windows\CALC.EXE. You can think of any directory or subdirectory as a trunk. Its tree consists of all subdirectories and files beneath the trunk (for an unexplained reason, directory trees grow upside down).
Now, back into the forest. Remember that Matchmaker compares the contents of two directory trees. The program's pairs of drive and directory list boxes allow us to select these trees by specifying their trunks. Matchmaker doesn't compare the trunks. It only compares the branches and leaves--the subdirectories and files--attached to the trunks.
To do that, it grafts the branches and trees of one trunk onto another. First, it traverses one tree (the one specified in the list boxes on the left-hand side of the main window). Starting at the trunk, it examines each subdirectory one at a time, identifying each file the tree contains.
Once it finds a file, Matchmaker grafts it onto the tree specified in the list boxes on the right-hand side of the main window. All the magic takes place in this one VB statement (see the code above): P2 = Trunk2 + Right(P1, (Len(P1) - TrunkLen1)).
Thanks to work done elsewhere, the variable P1 already contains the full pathname of a file in the left-hand tree. The variable Trunk1 contains the trunk of the left-hand tree, and the variable TrunkLen1 contains the number of characters in Trunk1. Similarly, Trunk2 contains the name of the right-hand tree, while TrunkLen2 contains the length of Trunk1.
Our statement calls VB's built-in function Right to remove the old trunk from P1. It does this by computing the length of P1, minus Trunk1 (Len(P1) - TrunkLen1), then asking Right for that many characters from the right-hand end of P1. VB's concatenation operation ("+") then attaches the trunk-less portion of P1 to Trunk2. Matchmaker finally stores the result in P2.
After forming two pathnames (P1 and P2), Matchmaker can easily compare their files. To see if a file exists, Matchmaker simply opens the file, then immediately closes it. If no errors occur, the file must exist. VB's Len function makes file size comparisons a snap. Pass the function the pathname of a file, and it returns the file's size in bytes. Similarly, VB's FileDateTime function accepts a pathname, and returns the file's date and time of last modification. Comparing two files' contents isn't much harder. Matchmaker simply opens both files and reads their contents 8KB at a time. After each read, it compares the data retrieved from each file to each other.
You may be wondering why Matchmaker doesn't always perform its full suite of compatibility tests. Why disable various comparisons? One reason is speed. If one or more of the directory trees reside on a slow device, such as an older CD-ROM drive, it will take longer to run the full program. Also, it's not always possible to compare file modification dates and times. DOS's COPY command preserves a file's timestamp when making a copy, but not all programs are so considerate. As a result, one directory tree can be identical to another except for each file's timestamp.
If you'd like to give Matchmaker a whirl, it's available from any of WinMag's download locations (you'll find them listed on the Table of Contents). The archive file PWMATCH.ZIP contains both source and executable versions. Feel free to use it as is, or modify it to your heart's content.
Contributing Editor Karen Kenworthy is the author of Visual Basic for Applications, Revealed! (Prima Publishing, 1994) and the manager of WINDOWS Magazine's forums on America Online and CompuServe. Contact Karen in the "Power Windows" topic of these areas, or care of the editor at the e-mail addresses here.