954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Common lines in text files

this is not a question, but an application to find common lines among text files, as long as you click "read new text file" button, it compares it to the listview and removes the uncommon items from the listview. I find this application very useful, hope you will like it too, as well as the project i also attach three sample files.

Form1.cs :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CommonText
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}
		private bool firstRead = true;
		private void btnReset_Click(object sender, EventArgs e)
		{
			listView1.Items.Clear();
			listView2.Items.Clear();
		}

		private void btnReadNew_Click(object sender, EventArgs e)
		{
			if (openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				listView2.Items.Add(Path.GetFileName(openFileDialog1.FileName));
				TextReader tr = new StreamReader(openFileDialog1.FileName);
				string input = null;
				if (firstRead)
				{
					while ((input = tr.ReadLine()) != null)
						listView1.Items.Add(input);
					tr.Close();
					firstRead = false;
				}
				else
				{

					string lines = File.ReadAllText(openFileDialog1.FileName);
					foreach (ListViewItem lvi in listView1.Items)
					{
						if (lines.Contains(lvi.Text) == false)
							listView1.Items.Remove(lvi);
					}
					
				}
			}
		}
		
	}
}


Form1.Designer.cs :

namespace CommonText
{
	partial class Form1
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.listView1 = new System.Windows.Forms.ListView();
			this.btnReadNew = new System.Windows.Forms.Button();
			this.listView2 = new System.Windows.Forms.ListView();
			this.label1 = new System.Windows.Forms.Label();
			this.btnReset = new System.Windows.Forms.Button();
			this.label2 = new System.Windows.Forms.Label();
			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.SuspendLayout();
			// 
			// listView1
			// 
			this.listView1.Location = new System.Drawing.Point(98, 79);
			this.listView1.Name = "listView1";
			this.listView1.Size = new System.Drawing.Size(310, 351);
			this.listView1.TabIndex = 0;
			this.listView1.UseCompatibleStateImageBehavior = false;
			this.listView1.View = System.Windows.Forms.View.List;
			// 
			// btnReadNew
			// 
			this.btnReadNew.Location = new System.Drawing.Point(114, 19);
			this.btnReadNew.Name = "btnReadNew";
			this.btnReadNew.Size = new System.Drawing.Size(122, 23);
			this.btnReadNew.TabIndex = 1;
			this.btnReadNew.Text = "Read New Text File";
			this.btnReadNew.UseVisualStyleBackColor = true;
			this.btnReadNew.Click += new System.EventHandler(this.btnReadNew_Click);
			// 
			// listView2
			// 
			this.listView2.Location = new System.Drawing.Point(7, 79);
			this.listView2.Name = "listView2";
			this.listView2.Size = new System.Drawing.Size(85, 159);
			this.listView2.TabIndex = 2;
			this.listView2.UseCompatibleStateImageBehavior = false;
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.Location = new System.Drawing.Point(13, 54);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(79, 13);
			this.label1.TabIndex = 3;
			this.label1.Text = "Compared Files";
			// 
			// btnReset
			// 
			this.btnReset.Location = new System.Drawing.Point(267, 19);
			this.btnReset.Name = "btnReset";
			this.btnReset.Size = new System.Drawing.Size(75, 23);
			this.btnReset.TabIndex = 4;
			this.btnReset.Text = "Reset";
			this.btnReset.UseVisualStyleBackColor = true;
			this.btnReset.Click += new System.EventHandler(this.btnReset_Click);
			// 
			// label2
			// 
			this.label2.AutoSize = true;
			this.label2.Location = new System.Drawing.Point(213, 54);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(76, 13);
			this.label2.TabIndex = 5;
			this.label2.Text = "Common Lines";
			// 
			// openFileDialog1
			// 
			this.openFileDialog1.FileName = "openFileDialog1";
			// 
			// Form1
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(441, 478);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.btnReset);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.listView2);
			this.Controls.Add(this.btnReadNew);
			this.Controls.Add(this.listView1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);
			this.PerformLayout();

		}

		#endregion

		private System.Windows.Forms.ListView listView1;
		private System.Windows.Forms.Button btnReadNew;
		private System.Windows.Forms.ListView listView2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button btnReset;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.OpenFileDialog openFileDialog1;
	}
}
Attachments CommonText.zip (34.79KB) hhp.txt (0.52KB) mc70.txt (0.47KB) ppt.txt (0.48KB)
serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 

there is a little thing i forgot :
modify the following accordingly :

private void btnReset_Click(object sender, EventArgs e)
		{
			listView1.Items.Clear();
			listView2.Items.Clear();
			firstRead = true;
		}
serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You