How to Compare two tsv or csv files


In this post I'm going to explaining about how to read data from *.TSV or *.CSV files and comparing these two files.

In this post I'm going to explaining about how to read data from *.TSV or *.CSV files and comparing these two files.

Step 1: Get All files from the folder. Next Loop through the all files.
Step 2: Read the data from one by one and construct Datatable template.
Step 3: After Loading the data into Datatable, comparing the 2 datatables.


// Declaration of Global Variables

private DataTable dataTable = null;
private DataTable dataTable2 = null;
private bool IsHeader = true;
private string headerLine = string.Empty;
private List AllLines;
private StringBuilder sb = new StringBuilder();
// ',' for CSV and '~','\t' for TSV files
private char seprateChar = ',';

///
/// Getting all files
///

private void CompareAllFiles()
{
String filename1, filename2, filePath, filePath2;

filePath = @"D:\Folder1\";
filePath2 = @"D:\Folder2\";

string[] AllfilePaths = Directory.GetFiles(filePath, "*.tsv");// *.csv
string[] AllfilePaths1 = Directory.GetFiles(filePath2, "*.tsv");
int iFCount = AllfilePaths.Length;
for (int i = 0; i < iFCount; i++)
{
filename1 = Path.GetFileName(AllfilePaths[i]);
filename2 = Path.GetFileName(AllfilePaths1[i]);
// reading data from files and loading into data table
dataTable = ReadDataFromFile(filePath + filename1, true, '~');
dataTable2 = ReadDataFromFile(filePath2 + filename2, true, '~');
// displaying data into datagrid
//dataGridView1.Visible = true;
//dataGridView1.DataSource = dataTable;
ComparingTwoFiles(dataTable, dataTable2);

}
}
///
/// Reading entire data from specified file path and loading into DataTable.
///

///
///
///
///
public DataTable ReadDataFromFile(string path, bool IsReadHeader, char serparationChar)
{
seprateChar = serparationChar;
IsHeader = IsReadHeader;
//Reading data from file by using StreamReader and loading into List
using (StreamReader sr = new StreamReader(path, Encoding.Default))
{
AllLines = new List();
while (!sr.EndOfStream)
{
AllLines.Add(sr.ReadLine());
}
//Method to create a table and load data into the data table
createTemplate(AllLines);
}

return dataTable;
}
///
/// Preparing DataTable Structure
///

///
///
private DataTable createTemplate(List lines)
{
List headers = new List();
dataTable = new DataTable();
if (lines.Count > 0)
{
string[] argHeaders = null;
for (int i = 0; i < lines.Count; i++)
{
if (i > 0)
{
DataRow newRow = dataTable.NewRow();
// others add to rows
string[] argLines = lines[i].Split(seprateChar);
for (int b = 0; b < argLines.Length; b++)
{
newRow[b] = argLines[b];
}
dataTable.Rows.Add(newRow);

}
else
{
// header add to columns
argHeaders = lines[0].Split(seprateChar);
foreach (string c in argHeaders)
{
DataColumn column = new DataColumn(c, typeof(string));
dataTable.Columns.Add(column);
}
}

}

}
return dataTable;
}
///
/// Comparing the two files
///

///
///
private void ComparingTwoFiles(DataTable dtFile1, DataTable dtFile2)
{
Boolean isIdentical = true;

if (dtFile1.Rows.Count == dtFile2.Rows.Count)
{
//applying sort before comparing
//Datatable 1
DataView dv = dtFile1.DefaultView;
dv.Sort = "EMPLOYEE ASC";
DataTable sortedDT = dv.ToTable();
//Datatable 2
DataView dv2 = dtFile2.DefaultView;
dv2.Sort = "EMPLOYEE ASC";
DataTable sortedDT1 = dv2.ToTable();


DataRow datarow1, datarow2;
int i;
//Traversing the data
//Reading row by row
for (i = 0; i < dtFile1.Rows.Count; i++)
{
//assigning the rows
datarow1 = sortedDT.Rows[i];
datarow2 = sortedDT.Rows[i];
int iCol = dtFile1.Columns.Count;
int iC;
string strCol, strCol1;
//Looping column by column
for (iC = 0; iC < iCol; iC++)
{
//assigning the column values
strCol = datarow1[iC].ToString().Trim();
strCol1 = datarow2[iC].ToString().Trim();
//comparing the values
if (strCol != strCol1)
{
isIdentical = false;
}
}

}

}
else isIdentical = false;
if (isIdentical)
MessageBox.Show(" Files are Identical:");
else
MessageBox.Show(" Files are Not Identical:");

}

I hope It'll helpful for you.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: