It is easy to compare two files of different version and get differences, using any tool or manual, but many times we have to compare multiple files or we require comparison report periodically. In this case, manual comparison takes a lot of time and it is also time-consuming and hectic to compare large number of files.
To get rid of above problem, we have developed a smart application which uses Beyond Compare tool’s script and C# code to compare as many files as you want.
Beyond compare is tool which provides user interface to compare files.
In this articles we will describe how can we use Beyond Compare Script in .net to automate compare files and generate report. In example below, we have used two different environments of website. Staging and Production environment, where we generate a report as below:
- List of unmatched files.
- List of files missing on Staging.
- List of files missing on Production.
Beyond Compare provide a command to compare files along with user interface, as below:
BCompare.exe @”Script.txt” “Path & Name of File1 or Folder” “Path & Name of File2 or Folder” “Path & Name of Report”
Bcompare.exe takes 4 parameters to compare files and generate report. Below is the description of parameters:
- Script.txt – This is just similar configuration or setting file which Beyond Compare uses while comparing files.
- Path & Name of File1.txt or Folder – File or folder to compare to.
- Path & Name of File2.txt or Folder – File or folder to compare with.
- Path & Name of Report– Comparison report. Report can be generated in text form or html form; depend on setting defined in Script.txt.
Below is the setting defined in our script file:
load “%1” “%2″
folder-report layout:summary options:display-mismatches output-to:”%3” output-options:html-color
Description for setting defined:
Now we need to create a console application in visual studio. Further we have to define path of Source and Destination folder to compare i.e. path of Staging and Production Environment, in App.Config file as shown below so that any time can we change in case an environment deployed somewhere else or same code can be used on different system. We can also define email configuration in App.Config file so that report can be emailed after comparison.
<add key=“Production“ value=“Path of Production Environment“/ >
<add key=“Script“ value=“Path of Script File“/ >
<add key=“Report“ value=“Path of report“/ >
Now write code in C# under main function of application.
String Staging = Convert.ToString(ConfigurationManager.AppSettings[“Staging“]);
String Production = Convert.ToString(ConfigurationManager.AppSettings[“Production“]);
String Report = Convert.ToString(ConfigurationManager.AppSettings[“Report“]);
String Script = Convert.ToString(ConfigurationManager.AppSettings[“Script“]);
System.Diagnostics.ProcessStartInfo startInfo =
startInfo.FileName = “cmd.exe”;
startInfo.FileName = “\”BCompare.exe\””;
startInfo.Arguments = “@\”” + Script + “\” \”” + Staging + “\” \”” + Production + “\” \”” + Report + “\””;
Let’s Run the above code and get the report. You can schedule it in windows scheduler to automate it.