Concept of Sync Tool

microkarl

Regular
Joined
Apr 22, 2004
Messages
88
Location
Morristown, NJ
All,
I am learning C#, so I want to create some simple program and learn it this way. The simple program that I am thinking is a backup/sync utility. However, there's a performance issue in my mind. If I have selected two folders: A, which is the target folder; and B, which is the destination folder. And both of them has some files that the other one does NOT have. Now, the way that I am implementing is as follow:

1: Compare folder A's files against B's file - (this means I am running a foreach statements on folder A now!!)
1.1: if A have files that are more updated, copy them into folder B; likewise, if B have more updated files, copy them into A. Moreover, put their names into an array.
1.2: if A have files that B does not have, copy them into folder B. Moreover, put their names into an array list.

2: Compare folder B's files against A's file - (this means I am running a foreach statements on folder B now!!)
2.1: Within the foreach statment, check B's files to see if their name are on the arraylist,
- if yes, this means they have been compared in 1.1 or they are added into B's folder recently in step 1.2.
- if no, this means B have some files A does not have. then copy them over to A.

This is the best way I can think of, what do you guys think? The main reason I don't like this approach is the two foreach statments and inside every loop of the foreach statment, it has to find the file from the other folder first... so I don't think this is effecient. Just imagine if you have couple hundred files in each folders.

And, yes, if you are thinking what the heck should I go through all these troubles while all I need to do is to make a .BAT files and do a XCOPY... well, it's because this is a good way for me to learn not only programming, but designing... so please gimme some hints.

Thanks,
Carl
 
Dictionary approach

I would start by creating two dictionaries (eg Hashtable, or Dictionary if using framework 2.0). Populate one dictionary with folder A's files, and the other with folder B's files, using the filename as the key. Then, when you loop through folder A's files (using the dictionary), you can quickly access the relevant file in folder B without needing to loop through them all. As you update files, remove them from folder B's dictionary and then when you loop through that, it will only contain the files which must be transferred to folder A.

eg:

C#:
//Instantiate dictionaries
dictA = new Dictionary<string,FileInfo>(); //Can also use Hashtable
dictB = new Dictionary<string,FileInfo>(); //Can also use Hashtable

//Populate dictionaries from actual folders here

//Loop through A's files
foreach (FileInfo fA in dictA.Values) {
    FileInfo fB;

    if (dictB.TryGetValue(fA.Name, out fB)) {
        //Exists in A and B, compare the two and copy appropriately

        //Remove from dictB
        dictB.Remove(fA.Name);
    } else {
        //Does not exist in B, copy from A to B
    }
}

//Loop through B's remaining files
foreach (FileInfo fB in dictB.Values) {
    //Copy fB to folder A
}

Good luck :cool:
 
MrPaul, first of all, thanks very much for the quick reply. When you say instantiate a dictionary object: dictA = new Dictionary<string,FileInfo>(); what does <string,FileInfo> mean here?

Besides, sorry that I am not really that good in .NET 2.0, but when you need to add somthing into either Hashtable or Dictionary, don't you have to loop through the folder, and add the object one by one first? For example:

Code:
foreach (fileinfo F in FolderA.files())
{
     objDictionary.Add(F.name);
}

So, if I do it this way, I will have to run a Foreach loop 4 times, (2 for filling the dictionary objects; the other 2 are for comparing...). Unless if there's some sort of built-in methods in the dictionary object in .net 2.0...

-- Carl
 
Simplification!

microkarl said:
Besides, sorry that I am not really that good in .NET 2.0, but when you need to add somthing into either Hashtable or Dictionary, don't you have to loop through the folder, and add the object one by one first?

Yes you would - I did not include the code for that part. It occured to me after posting that you really only need use a Dictionary for folder B's files, as the Dictionary for folder A is not used (apart from iteration). So the code could be simplified to:

C#:
//Instantiate dictionary
dictB = new Dictionary<string,FileInfo>(); //Can also use Hashtable
 
//Populate dictionary from folder B here
foreach (FileInfo fB in folderB.Files)
    dictB[fB.Name] = fB;
 
//Loop through A's files
foreach (FileInfo fA in folderA.Files) {
    FileInfo fB;
 
    if (dictB.TryGetValue(fA.Name, out fB)) {
        //Exists in A and B, compare the two and copy appropriately
 
        //Remove from dictB
        dictB.Remove(fA.Name);
    } else {
        //Does not exist in B, copy from A to B
    }
}
 
//Loop through B's remaining files
foreach (FileInfo fB in dictB.Values) {
    //Copy to folder A
}

That is 3 loops, but only one of them needs to compare the file details, the first just populates a Dictionary and the last just copies files. Also, your original approach involved some 'hidden' loops such as that required to find if a file appears in the ArrayList. Overall I would expect this approach to be more efficient.

microkarl said:
When you say instantiate a dictionary object: dictA = new Dictionary<string,FileInfo>(); what does <string,FileInfo> mean here?

Generic classes were introduced in .Net 2.0. Basically here we are declaring that we want a Dictionary class that stores FileInfo objects with an associated string (the filename). I'm not going to explain all the ins and outs of generics but I'm sure you can find information here on this forum or elsewhere.

Good luck :)
 
Back
Top