Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to compare the contents of folders including subdirectories Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
18-Oct-02
Category
Files Operation
Language
Delphi 2.x
Views
108
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

I need to compare the file contents of a folder and its subfolders on all Win98 
machines at my client's site (each machine was setup differently but, over time, 
has had different patches applied to the program in question). I need to report 
back only the differences found in files based upon Time/ Date stamp. Is there a 
good way to do this?

Answer:

You use a recursive scanning loop (FindFirst/ FindNext/ FindClose) starting at the 
topmost folder you need to examine. The loop stores each file it find into a 
TStringlist. It stores a relative path to the start folder. For each file found it 
also stores the searchrec.time into the Objects property of the stringlist (it uses 
AddObject instead of Add). It will fit with a little typecast since it is four 
bytes, like an object reference. After the end of the scan you have a list of all 
files, which you can now write to disk for your reference computer to produce a 
master list that will be used on the other PCs to find the differences. The output 
file needs to contain the timestamp, of course, so it would be produced with 
something like:
1   
2   procedure SaveScan(files: TStringlist; const filename: string);
3   var
4     f: textfile;
5     i: Integer;
6   begin
7     assignfile(f, filename);
8     rewrite(f);
9     try
10      for i = 0 to files.count - 1 do
11        writeLn(f, Format('%p%s', [pointer(files.Objects[i]), files[i]]));
12    finally
13      closefile(f);
14    end;
15  end;
16  
17  //Reading the list back would be:
18  
19  procedure LoadScan(files: TStringlist; const filename: string);
20  var
21    f: textfile;
22    S: string;
23  begin
24    assignfile(f, filename);
25    reset(f);
26    try
27      files.clear;
28      while not EOF(f) do
29      begin
30        ReadLn(f, S);
31        files.AddObject(Copy(S, 9, Maxint), TObject(StrToInt('$' + Copy(S, 1, 8))));
32      end;
33    finally
34      Closefile(f);
35    end;
36  end;


Ok, on the other PCs you repeat the scan to build the list of files on that PC, you 
load the master list into another TStringlist, sort both lists and then compare 
them item by item. How complex that can get depends on what kinds of differences 
you expect to find. If there can be missing and extra files in addition to changed 
ones it gets a bit intricate but not too daunting. It goes like this:

You define two counters for the two lists, lets call them mi for the master list 
and li for the "local" list to compare it to. Both start out at 0.

37  while (mi < masterlist.count) do
38  begin
39    if masterlist[mi] = locallist[li] then
40    begin
41      {compare the two objects properties, if not equal report the file as changed}
42      Inc(mi);
43      Inc(li);
44    end
45    else if masterlist[mi] < locallist[li] then
46    begin
47      {report masterlist[mi] as missing}
48      Inc(mi);
49    end
50    else
51    begin
52      {report locallist[li] as extra}
53      Inc(li);
54    end;
55    if mi >= masterlist.count then
56      {report any remaining files in locallist as extra}
57      if li >= locallist.count then
58        {report any remaining files in masterlist as missing and increment mi for 
59  each,
60  			 so the loop is terminated}
61  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC