Monday 15 February 2010

C# Comparing two files and exporting matching lines based on delimiter -


Here's the scenario.

I have a text file (alpha), one column, of items with a bunch.

My second file is a CSV (Delta) that has 4 columns I

I have to compare the delta with alpha and create a new file (omega) in which the alpha delta , It will export only the first two columns from Delta to a new .txt file.

Example: (alpha) BeginID / P> P> This document may contain 10k lines. Thanks for the help!

Here's a rough cut method:

  System Use; Using System.Collections.Generic; Using System.IO; Using System.Text; Namespace ConsoleApplication1 {class program {static void key (string [] args) {String alphaFilePath = @ "C: \ Documents and Settings \ Jason \ My Documents \ Visual Studio 2008 \ Projects \ Compte Two Files \ Compte Two Files \ ExternalFiles \ Alpha .text "; & Lt; String & gt; Alpha content = new list & lt; String & gt; (); Using (FileStream FS = New FileStream (alphaFilePath, FileMode.Open)) (StreamReader RDR = New StreamReader (FS)) {while (rdr.EndOfStream!) {AlphaFileContent.Add (rdr.ReadLine ()) ; }} String betaFilePath = @ "C: \ beta.csv"; Stringbilder sb = new stringbiller (); Using (FileStream FS = New FileStream (BetaFilePath, FileMode.Open)) (StreamReader RDR = New StreamReader (FS)) {While (! Rdr.EndOfStream) {string [] betaFileLine = rdr.ReadLine () . Split (replace. Tochar (",")); If (alphaFileContent.Contains (betaFileLine [0]) {sb.AppendLine (String.Format ("{0}, {1}", betaFileLine [0], betaFileLine [1]); }}} (Using the file stream FS = new filestream (@ "c: \ omega.ttec", fliemmode.creative)) (stream writer author = new streamer (FS)) {writer.Write (sb) .toString ()); } Console. WrightLine (SBthostring ()); }}}  

Actually it reads a txt file, puts content in a list, then it reads a CSV file (no column considers) and values ​​to create a stringbilder Corresponds to In your code, replace the stringbilder with the creation of a new txt file.

EDIT: If you wish to run the code in a button click, then click on it button to call the handler (or a new routine):

  Public Zero Button Clicks (Object Sender, EventArgs E) {string alphaFilePath = @ "C: \ Documents and Settings \ Jason My Documents \ Visual Studio 2008 \ Projects \ Compte Two Files \ Compat two files \ ExternalFiles \ Alpha.txt"; & Lt; String & gt; Alpha content = new list & lt; String & gt; (); Using (FileStream FS = New FileStream (alphaFilePath, FileMode.Open)) (StreamReader RDR = New StreamReader (FS)) {while (rdr.EndOfStream!) {AlphaFileContent.Add (rdr.ReadLine ()) ; }} String betaFilePath = @ "C: \ beta.csv"; Stringbilder sb = new stringbiller (); Using (FileStream FS = New FileStream (BetaFilePath, FileMode.Open)) (StreamReader RDR = New StreamReader (FS)) {While (! Rdr.EndOfStream) {string [] betaFileLine = rdr.ReadLine () . Split (replace. Tochar (",")); If (alphafile content. (BetaPileline [0]) {sb.AppendLine (string.format ("{0}, {1}", betafileline [0], betafileline [1]);}}} Using the file stream FS = new filestream (@ "c: \ omega.ttec", fliemmode.creative)) (stream writer author = new streamer (FS)) {writer.Write (sb.ToString ())}}  

No comments:

Post a Comment