+ Reply to Thread
Results 1 to 2 of 2

Thread: Text file line splitter and removing duplicates

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    34

    Text file line splitter and removing duplicates

    I have a file with millions of emails, the emails format in the file are like: account@msn.com , jack@yahoo.com, username@mail.com. I like each mail to be in a separate line and remove duplicates, remove spaces and finally adding an option to save it as .txt or .csv. I tried to do that, but i dont have a great knowledge on IO , so i was wondering if you guys could help. Thanks.

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    40
    Hello, i`ll be more than happy to help you with that.

    Here is the code in C#:
    Code:
    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 txt_file_line_splitter_and_delete_duplicated
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void split(string filename)
            {
                string phase1 = "C:/p1.txt";
                StreamWriter sw1 = new StreamWriter(phase1);
    
                foreach (string line0 in File.ReadAllLines(filename))
                {
                    string[] wordsinline = line0.Split(',');
    
                    foreach (string word in wordsinline)
                    {
                        sw1.WriteLine(word);
                        sw1.Flush();
                    }
                }
                sw1.Close();
            }
            private void removespace()
            {
                string phase1 = "C:/p1.txt";
                string phase2 = "C:/p2.txt";
    
                StreamWriter sw2 = new StreamWriter(phase2);
                StreamReader sr1 = new StreamReader(phase1);
    
                string line1;
    
                while ((line1 = sr1.ReadLine()) != null)
                {
                    sw2.WriteLine(line1.Replace(" ", ""));
                    sw2.Flush();
                }
                sw2.Close();
                sr1.Close();
     
            }
            private void removep1()
            {
                string phase1 = "C:/p1.txt";
                File.Delete(phase1);
            }
            private void removedeblicated()
            {
                string phase2 = "C:/p2.txt";
                string phase3 = textBox2.Text;
    
                StreamWriter sw = new StreamWriter(phase3);
    
                List<string> list = new List<string>();
    
                foreach (string line0 in File.ReadAllLines(phase2))
                {
                    if (!list.Contains(line0))
                    {
                        list.Add(line0);
                    }
                }
    
                foreach (string item in list)
                {
                    sw.WriteLine(item);
                    sw.Flush();
                }
                sw.Close();
            }
            private void removep2()
            {
                string phase2 = "C:/p2.txt";
                File.Delete(phase2);
            }
            private void saveitas()
            {
                if (radioButton1.Checked)
                {
                    textBox2.Text = textBox2.Text + "/Final-Split-File.txt";
                }
                else if (radioButton2.Checked)
                {
                    textBox2.Text = textBox2.Text + "/Final-Split-File.csv";
                }
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                if (textBox1.Text == "")
                {
                    MessageBox.Show("Choose The file you want to split", "0X001", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                if (textBox2.Text == "")
                {
                    MessageBox.Show("Where to save ??","0X002",MessageBoxButtons.OK,MessageBoxIcon.Error);
                }
                if (!radioButton1.Checked && !radioButton2.Checked)
                {
                    MessageBox.Show("Save it as .txt or .csv ? please choose", "0X003", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    try
                    {
                        split(textBox1.Text);
                        removespace();
                        removep1();
                        saveitas();
                        removedeblicated();
                        removep2();
    
                        MessageBox.Show("Done splitting your file , you can check it now", "Done!", MessageBoxButtons.OK, MessageBoxIcon.Information);
    
                        textBox1.Text = "";
                        textBox2.Text = "";
                    }
                    catch
                    {
                        MessageBox.Show("An error occurred , application will restart");
                        Application.Restart();
                    }
                }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                OpenFileDialog ofd1 = new OpenFileDialog();
                ofd1.ShowDialog();
                textBox1.Text = ofd1.FileName;
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                FolderBrowserDialog fbd = new FolderBrowserDialog();
                fbd.ShowDialog();
                textBox2.Text = fbd.SelectedPath;
            }
        }
    }
    Last edited by jacklin; 01-09-2012 at 11:03 AM.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts