Subscribe to Windows IT Pro
April 10, 2006 12:00 AM

Detect Directory Differences

Perl script makes comparing files easy
Windows IT Pro
InstantDoc ID #49728
Rating: (0)
Downloads
49728.zip

When the script can open the directory specified by $Path, each object in that directory is enumerated. The script constructs a string that ensures the object's file path won't wrap on screen and assigns that string to the $Pretty-Path variable. The script prevents wrapped file paths by calling the AbbreviatePath() subroutine, which replaces enough characters in the string with an ellipsis (...) to ensure that the string is short enough to fit on a printed line. The $PrettyPath string is then printed to STDOUT to indicate the script's progress. Knowing the progress is important when you use the script to analyze large numbers of files.

Next, DirDiff.pl checks to see whether each object is a directory. If an object is a directory, the script adds it to the @DirList array for later processing. If an object is a file, the script retrieves the file's size (unless the user includes the -l option, one of the command-line options that I'll discuss shortly) and stores the file information in the %FileList hash. If the user wants directory recursion (which the user specifies with the -s command-line option), the script enumerates each directory in the @DirList array and recursively calls Collect-FileList() for it.

Although not evident by its name, the PrintReport() subroutine does more than just display the script's results. This subroutine also performs the directory comparison. As callout D shows, this subroutine starts by resetting $gFileCount so that it can be used to determine the difference in the number of files in the directories. After writing the report's header, the subroutine sets the format for the file data. Then, the subroutine prepares for the directory comparison by enumerating the full list of file paths in the %FileList hash. For each path, the subroutine determines whether the file was present in directory 1 or directory 2 and retrieves the file's size.

The code at callout E is where the PrintReport() subroutine compares the files in the two directories. The subroutine first checks to see whether each enumerated file is present in both directories. When a file is in only one directory, the subroutine reports this discrepancy. When a file is in both directories, the subroutine checks to see whether the file in directory 1 is same size as the file in directory 2. If there's a discrepancy (i.e., the size differs), the %File hash entries' strings are modified to include the file size. Finally, the subroutine uses the write command to print the discrepancies on screen.

Using the Script
To run DirDiff.pl, you need to pass in the paths to the two directories you want to compare. For example, to display the differences between the C:\Dir1 and C:\Dir2 directories, you run the command

Perl DirDiff.pl C:\Dir1 C:\Dir2 -s 

The analysis will include all subdirectories. If you don't want recursion into the subdirectories, simply omit the -s option.

Because enumerating thousands of files and retrieving their file sizes can take a considerable amount of time (especially if any of the directories are on a remote share on a network), it's useful to disable the file-size analysis by including the -l option. For example, to compare only the names of the files on \\server_a\share_1 and \\server _b\share_1, you run the command

Perl DirDiff.pl
  \\server_a\share_1  \\server_b\share_1 -s -l 

(Although this command appears on two lines here, you would enter it on one line in the command-shell window.) I wrote and tested DirDiff.pl on machines running Windows Server 2003, Windows XP, and XP 64-Bit Edition.

A Simple But Valuable Tool
Since I wrote DirDiff.pl, I've found that I use it much more than I previously suspected. Although the logic of the script is rather simple, its value is quite high.

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.