my my github
karthik25 forked EightMedia/hammer.js
Forked repository is at karthik25/hammer.js

karthik25 pushed to master at karthik25/aspnet-mvc-checkboxlist

karthik25 pushed to master at karthik25/aspnet-mvc-checkboxlist

karthik25 created branch master at karthik25/aspnet-mvc-checkboxlist

karthik25 created repository aspnet-mvc-checkboxlist
ASP.Net MVC 3 CheckBoxList Extension Sample project


Directory Comparer
26 downloads

intro


directory comparer, as the name implies, is a tool to compare folders. i have discussed this application in a way which i feel would be pretty helpful. it tries to introduce a few concepts apart from providing some tips. when i started with this project, it had limited features, but as time porgressed, i incorporated a lot more features. directory comparer is extensible so you always have the option of creating your own comparers or own ui for representing the results of the default comparers provided.


background


yes, out there are a few directory comparers. but i just wanted something that is simple and straight-forward. this is what i define as "simple" - choose folder 1, choose folder 2, click on button, I should be done. so, I just came up with this recurive comparison tool. i also wanted it to be extensible and so the core functionality is implemented using interfaces, so that it is truly extensible.


core logic behind directory comparer


at the heart of it, directory comparer has the RecursiveComparer. it implements the ITwoPassComparer interface, which, as the name suggests handles the comparison in 2 phases. they are (1) comparison with respect to the left folder and (2) comparison with respect to the right folder. i would say any complex problem could be simplified in to multiple simple problems. if you think of it, left comparison would do bulk of the comparison efforts. the only thing right comparison has to do is to deal with files that are available only in the right. and an important point to note in this case is that everything that the recursive comparer does starts with the file name and it's case sensitive. i will try to simplify the logic i use here (assuming the "Recusive" checkbox is checked):

  • start with the left folder. get a list of all files and folders in the root directory
  • loop through the items one by one. If it's a file, call the method to process it. this method goes like
    • for the current file see if a corresponding file exists in the other side
    • if a file exits, compare the 2 files, create an entry and add it to a list that tracks comparisons
    • if there is no match, create an entry to indicate this and add it to a list that tracks comparisons
  • if it's a folder, call a method which is a recusrive method to get comparison information about more files / folders within the current folder
    • for files within the folder, it's processed as decribed above
    • for folders, the recursive process described here is followed
    • if any folder is an empty folder, an entry is added even for this case. This is indicated by folder icon to the extreme left (more discussion about this follows)
  • once the folder comparison with respect to the right folder starts I just have to deal with files/folders that exists only on the right

comparison of files is performed in 2 stages. first stage is finding a match by file name. second stage is by computing a hash (md5) of the left and right files. if either the left or right side of the file is not present, hash is not calculated at all, so as to save some time.


few words about the ui


the ui layout is pretty simple. at the top I have a menu bar with various menus and the corresponding drop-down items. next to this i have a list view which is set to fill the entire parent, which is the main form. apart from this, I have added SaveFileDialog to enable the use to save the comparison results as xml or csv and a ContextMenuStrip control which is used to provide the user with some options when any of items in the list view is selected. i felt that the ListView is the best option to present the user with the comparison information.


extending directory comparer


let's say you don't want to use my logic i explained above. you could always extend directory comparer in a way you want. you could do this in a few simple steps listed below:

  • implement the IResults interface (let's call this ImprovedComparisonResults)
  • implement the ITwoPassCompiler interface (let's call this ImprovedComparer)
  • implement the IDirectoryComparer interface (let's call this ImprovedDirectoryComparer)

once you are done with creating the above mentioned classes, you can make the ui to use that to render the results in the following way by modifying comparerWorker_DoWork method.

    private void comparerWorker_DoWork(object sender, DoWorkEventArgs e)
    {
	    ITwoPassComparer comparer = new ImprovedComparer(this);
	    IDirectoryComparer improvedComparer = new ImprovedDirectoryComparer(comparer);
	    IResults results = improvedComparer.CompareDirectories();

	    this.ReportProgress(100);

	    Thread.Sleep(1000);

	    e.Result = results;
    }
    

thus, in case you wished to play around with the directory comparer by using its ui rendering, you could follow the above procedure.

even the other way is possible. that is, you could write your own UI using the following method. instead of instantiating the frmCompareResults form in the comparerWorker_RunWorkerCompleted method, you could instantiate your own form to display the results as intended. an example is given below:

    private void comparerWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
	    IResults results = (IResults)e.Result;
	
	    _frmNewCompareResults = new frmNewCompareResults();
	    _frmNewCompareResults.Results = results;
	    _frmNewCompareResults.mainReference = this;
	    this.Hide();
	    _frmNewCompareResults.Show();
    }
    

comments


no comments have been posted for this project. please use the form below to post your comment.


post your comment



name (won't be published)
email (won't be published)
your comment
are you human?