The magazine of the Melbourne PC User Group

Compare All Files Or Selected Files In Any Two Directories
Keith Phillips

The "Match Two Files" programs in PC Update, August 2001 (http://www.melbpc.org.au/pcupdate/2108/2108article8.htm) compare any two files, even if the names are different. The "MULTI MATCH Files" programs described here compare several files in a directory with files of the same name in another directory. Use the same C:\QB and C:\CLU directories that were made for the MATCH programs and use the same methods to make SOURCE.BAS, MULTI.BAS and MULTI.BAT in C:\CLU.
 
Make the shortcut "MULTI MATCH Files" in Windows Explorer, open the C:\Windows\SendTo directory. Right click the "Match Two Files" shortcut and in the pop-up menu select "Create Shortcut". Rename the new shortcut "MULTI MATCH Files" and edit its Program tab, Font tab and Screen tab (Figures 1, 2 and 3). Click "Apply" and "OK". The shortcut has a blank working text field so the directory selected in Windows Explorer is the working directory.
 
Select a Directory

There are three ways to select a directory so that the highlight acts as a marker by staying in the selected directory.
  1. In the LEFT pane of Windows Explorer, LEFT click a directory to highlight it and RIGHT click the same directory to pop-up its context menu. If you omit the left click, the highlight will move back to whatever icon is shown as an open folder.
  2. In the RIGHT pane of Windows Explorer, RIGHT click a directory to high-light it and pop-up its context menu.
  3. The context menu of the root directory doesn't include "SendTo".
In the RIGHT pane of Windows Explorer RIGHT click any file in the root directory to highlight it and pop-up its context menu. The program uses the file's location to select the directory. This option is the only way to select the root directory but it also works in other directories. 
When the selected folder is highlighted and the context menu is displayed, "SendTo" the "MULTI MATCH Files" shortcut.


Figure1. The program tab.


Figure 2. The font tab.


Figure 3. The screen tab.

Select The Source Files

The source files can be on any drive. When you "SendTo" the "MULTI MATCH Files" shortcut, a list is displayed in Notepad. The first two lines are instructions. The third line is the path which ends with a backslash. Then follows the list of ALL THE FILES in the directory (Figure 4).
 
If instead of displaying the list in Notepad, the program starts comparing files, it means the source list from a previous comparison has not been cleared. Press Esc to delete the source list and start again.


Figure 4. The source files displayed in Notepad

To compare all files in the list, leave the list unaltered and close Notepad. If you don't want to compare all the files, edit the list: delete the file names you don't want AND/OR put a space before any file name to exclude it AND/OR put a blank line in the list to exclude all subsequent file names. Save the edited list and close Notepad. The highlight remains on the selected source directory or file.
 
Select The Target Directory

The target files can be on any drive. When you select a directory or file and "SendTo" the "MULTI MATCH Files" shortcut, a separate report is displayed for each file. If a file doesn't match you can press the Y key to save the report in Notepad which appears as a minimised Task Bar Button so it doesn't obscure the display screen. When the Task Bar Button appears a short beep sounds. When the display screen is closed the selected source directory or file remains highlighted in Windows Explorer. If you accidentally select the same file twice, an error message appears in Notepad and the selection is cancelled. Start again.

This program was tested by copying all the files shown in Figure 4 into a new directory named C:\MPCKIT2.


Figure 5. Results in the QBASIC display

To illustrate how a mismatch is reported the copy of README.TXT in MPCKIT2 was altered by typing in a row of Xs. The list was edited by putting spaces before the 2nd, 3rd & 4th file names. The result is shown in the QBASIC display, Figure 5 above.

Figure 6. The "source" code(source can be downloaded from HERE)
COLOR 17, 15: CLS
OPEN "C:\CLU\DIR-LIST" FOR INPUT AS #1
  FOR i = 1 TO 4: LINE INPUT #1, tmp$: NEXT i
CLOSE #1: path$ = MID$(tmp$, 15) 'Path to source
  cmd$ = "Dir " + CHR$(34) + path$ + CHR$(34) + " /A-D /ONE >C:\CLU\DIR-LIST"
OPEN "C:\CLU\TEMP.BAT" FOR OUTPUT AS #1 'Batch prog
  PRINT #1, "@echo off": PRINT #1, cmd$
CLOSE #1: SHELL "C:\CLU\TEMP.BAT" 'Run batch prog
KILL "C:\CLU\TEMP.BAT" 'Delete batch program
OPEN "C:\CLU\SOURCE" FOR OUTPUT AS #1 'Make SOURCE
  PRINT #1, "Space BEFORE FILE = Exclude That File"
  PRINT #1, "Blank Line BEFORE FILE = Exclude Rest"
  OPEN "C:\CLU\DIR-LIST" FOR INPUT AS #2: i = 0
    DO: i = i + 1: LINE INPUT #2, tmp$
      IF i = 4 THEN 'Path to source dir and files
        tmp$ = MID$(tmp$, 15) 'tmp$ is the path
        IF LEN(tmp$) > 3 THEN tmp$ = tmp$ + "\"
        PRINT #1, tmp$ 'Save path to source
     ELSEIF i > 5 AND LEFT$(tmp$, 1) <> " " THEN
       PRINT #1, MID$(tmp$, 45) 'File names
     END IF
  LOOP UNTIL EOF(2): CLOSE 'Close both #1 & #2
SHELL "C:\Windows\Notepad.exe C:\CLU\SOURCE"
SYSTEM
 
Figure 7. The batch file MULTI.BAT  (source can be downloaded from HERE)
@echo off
if exist C:\CLU\SOURCE goto use-source
rem ** SOURCE DOES NOT EXIST SO MAKE IT **
dir %1 /a-d>C:\CLU\DIR-LIST
C:\QB\QBASIC.EXE /RUN C:\CLU\SOURCE.BAS
rem ** SOURCE.BAS WILL MAKE C:\CLU\SOURCE **
goto end
:use-source
rem ** C:\CLU\SOURCE DOES EXIST SO USE IT **
dir %1 /a-d>C:\CLU\DIR-LIST
C:\QB\QBASIC.EXE /RUN C:\CLU\MULTI.BAS
rem ** MULTI.BAS WILL MATCH THE SOURCE FILES **
:end
 
Figure 8. The Code Listing MULTI.BAS  (source can be downloaded from HERE)
MULTI.BAS

COLOR 17, 15: CLS
PRINT " MULTI MATCH Files": PRINT STRING$(80, 223)
OPEN "C:\CLU\DIR-LIST" FOR INPUT AS #1
  FOR i = 1 TO 4: LINE INPUT #1, tmp$: NEXT i
CLOSE #1: path2$ = MID$(tmp$, 15) 'Path to target
IF LEN(path2$) > 3 THEN path2$ = path2$ + "\"
OPEN "C:\CLU\SOURCE" FOR INPUT AS #2: i = 0
  DO: i = i + 1: LINE INPUT #2, tmp$ 'Foldername(s)
    IF i = 3 THEN 'Line 3 is path to source
      path1$ = tmp$: GOSUB VerifyPaths
    ELSEIF tmp$ = "" THEN 'The line is blank
      EXIT DO 'Get no file names below blank line
    ELSEIF i > 3 AND LEFT$(tmp$, 1) <> " " THEN
      GOSUB UseFileName 'File name is flush left
    END IF 'Skip all file names preceded by space
  LOOP UNTIL EOF(2)
CLOSE #2

PRINT STRING$(80, 223): PRINT "Finished !"
PRINT "FIRST PressAnyKey To Close ! THEN Open Any [Results-Notepad]Task Bar Buttons"
PRINT STRING$(80, 223): press$ = INPUT$(1)
KILL "C:\CLU\SOURCE": SYSTEM 'Finished-Close screen

VerifyPaths:
  IF path1$ = path2$ THEN
    COLOR , 12: PRINT "Source & Target Paths Are The Same. They Must Be Different!"
   PRINT "PressAnyKey ": press$ = INPUT$(1)
   CLOSE #2: KILL "C:\CLU\SOURCE": SYSTEM 'Close
  END IF
RETURN

UseFileName: 'Uses only flush left files
  filename$ = tmp$: GOSUB MakeCommandLine
  GOSUB ExecuteCommandLine 'Show report on screen
  IF SCREEN(CSRLIN - 2, 1) = 70 THEN 'Files match
    LOCATE CSRLIN - 1, 1: PRINT "Press AnyKey/Esc"
    PRINT STRING$(80, 223): press$ = INPUT$(1)    
     IF press$ = CHR$(27) THEN
       CLOSE #2: KILL "C:\CLU\SOURCE": SYSTEM 'Close
     END IF
   ELSE 'Files different
     LOCATE CSRLIN - 2, 1
       PRINT "The Files Do Not Match. Put The Report Into Notepad? Press Y/N"
     PRINT STRING$(80, 223)
     press$ = UCASE$(INPUT$(1))
     IF press$ = "Y" THEN
       cmd$ = cmd$ + " >C:\CLU\RESULTS"
       GOSUB ExecuteCommandLine
         SHELL "Start /m C:\Windows\Notepad.exe C:\CLU\RESULTS"
       SOUND 1500, 9: SLEEP 1
    END IF
  END IF
RETURN

MakeCommandLine:
  file1$ = CHR$(34) + path1$ + filename$ + CHR$(34)
  file2$ = CHR$(34) + path2$ + filename$ + CHR$(34)
    cmd$ = "C:\Windows\Command\Fc.exe " + file1$ + " " + file2$
RETURN

ExecuteCommandLine: 'Put cmd$ into a batch program
  OPEN "C:\CLU\TEMP.BAT" FOR OUTPUT AS #1
    PRINT #1, "@echo off": PRINT #1, cmd$
  CLOSE #1: SHELL "C:\CLU\TEMP.BAT" 'Run batch prog
  KILL "C:\CLU\TEMP.BAT" 'Delete batch program
RETURN

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