aspose file tools*
The moose likes I/O and Streams and the fly likes Compare two directories Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Compare two directories" Watch "Compare two directories" New topic
Author

Compare two directories

Waze Mvarume
Greenhorn

Joined: Mar 14, 2004
Posts: 3
Hi guys,
I am really cracking my head over this one. If you have two dirs, how can you compare the two? ie. does the second dir match the template? I am looking at passing the two directories as arguments at command line. I can do the simple part like recursively going thru directory and subdirectories. Any ideas??? Below is my code so far.
Thanks in advance
Waze

[ March 15, 2004: Message edited by: Waze Waze ]
*Added code tags for easier reading -- Jason*
[ March 15, 2004: Message edited by: jason adam ]
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
A couple of things:
First, welcome to the Ranch! However, we do have a Naming Policy that states not obviously fictitious names. Most people don't have the same first and last name. You don't have to use your real last name, just something that "looks" real. Please change your display name in your profile. Thanks!

As far as your question, that depends on how you want to compare the files. Are you looking for files with the same name, same size, both, same data, etc? If you are just looking for file names, you can get the name of the current file and do an equals() for all the files in the array list. Personally I would deal with them as a collection and do a contains().
But if you want to also look at size, data, etc. then you have a bit more code to write.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If you have two lists sorted by name, you can do something like this:

See how it figures out if a file is in A only or B only? You might write a couple lists of files, mostly matching and a few unique and try the algorithm. In "files match" you might do more digging to compare dates, sizes, contents, etc.
This doesn't show how to handle the ends of the list. Let's add a bit more detail. Say when we "get A" and there are no more files in the A list we set a flag A-Done.

To get really elaborate, back in the 80s I wrote a Pascal program that let you specify the action in the "do A only" and B only and match routines. There are 14 choices, something like:

I still use the program and I think I've actually used all those choices over the years.
Hope that was interesting.
[ March 15, 2004: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Waze Mvarume
Greenhorn

Joined: Mar 14, 2004
Posts: 3
Thanks a lot for the great responses guys. Yes, I am looking for files with same name only. I haven't worked with lists and collections before so I am gonna read on on that and then try your suggestions. Right now I am trying to use the equals() method in a test method (see below) to see how I can match the string elements in arrays.
Will post an update soon.
My apologies I think I am too "green"!!
void doSearch(){
String[] list1 = {"Sisco","Peter","Edgar","Kenneth","Haroon","Kill","Bill"};
String[] list2 = {"Mpho","Lerato","Edgar","Kenneth","Haroon","Tatenda","Bill"};
Arrays.sort(list1);
Arrays.sort(list2);
for (int i=0;i<list1.length;i++){
for (int j=0;j<list2.length;j++){ //check in second list
if (list2[j].equals(list1[i])){
System.out.println(list2[j] + " " + " matches "+list1[i]);
}
/*if (!list2[j].equals(list1[i])){
System.out.println(list2[j] + " " + "has no match "+list1[i]);
}*/

}

}
}//end doSearch method
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
In your above example, for me it would be preferable to use collections.
You would have something like:

However, when dealing with files, the API says that equals compares the abstract pathname of the two files (and the contains() method utilizes the equals() method). I think that this means unless the two files are in the exact same location, they won't be equal. You'd need to separate out the actual file name, minus the path, and compare those. Might just be easier to use the arrays and call getName() on each.
 
Don't get me started about those stupid light bulbs.
 
subject: Compare two directories