All pastes #730357 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-10-09 03:48 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Timers;

namespace DuplicateKiller
{
    class Program
    {
        static int count = 0;
        static int killed = 0;
        static void Main(string[] args)
        {
            Timer tmr = new Timer();

            tmr.Interval = 1000;
            tmr.Elapsed += new ElapsedEventHandler(tmr_Elapsed);
            tmr.Start();

            args = new string[] { @"C:\Documents and Settings\Moose\Desktop\Work Stuff\info\leads.txt" };

            if ((args.Length == 0) || !File.Exists(args[0]))
            {
                Console.WriteLine("You must supply arguments by dragging and dropping a file on to the executable");
                Console.WriteLine("Press [ ENTER ] to exit");
                Console.ReadLine();
                Environment.Exit(0);
            }

            FileStream inStream = new FileStream(args[0], FileMode.Open, FileAccess.Read, FileShare.Read);
            TextReader tr = new StreamReader(inStream);

            TextWriter tw;

            Console.WriteLine("Working...");

            string data = "placeholder";

            int ct = 1;

            string[] dataArray = null;

            while ((data = tr.ReadLine()) != null)
            {
                dataArray = data.Split('\t');

                if (!isDuplicate("out.txt", dataArray, ct))
                {
                    tw = new StreamWriter("out.txt", true);
                    tw.WriteLine(data);
                    tw.Flush();
                    tw.Close();
                    count++;
                }

                ct++;
            }

            tr.Close();

            Console.WriteLine("Finished!");
            Console.WriteLine("Press [ ENTER ] to exit");
            Console.ReadLine();
        }

        static void tmr_Elapsed(object sender, ElapsedEventArgs e)
        {
            Console.Clear();
            Console.WriteLine("Added: {0}", count);
            Console.WriteLine("Killed: {0}", killed);
        }

        static bool isDuplicate(string file, string[] entry, int line)
        {
            FileStream stream = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Read);
            TextReader tr = new StreamReader(stream);

            string data = "placeholder";

            int ct = 1;
            string[] dataArray = null;

            bool isDuplicate = true;

            if (line == 1)
            {
                tr.Close();
                return false;
            }

            while ((data = tr.ReadLine()) != null)
            {
                dataArray = data.Split('\t');

                if (dataArray.Length == entry.Length && line != ct)
                {
                    if (dataArray[5] != entry[5] && dataArray[7] != entry[7])
                    {
                        isDuplicate = false;
                    }
                    else
                    {
                        killed++;
                        tr.Close();
                        return true;
                    }
                }

                ct++;
            }

            tr.Close();

            return isDuplicate;
        }
    }
}