EvilZone

Programming and Scripting => .NET Framework => : Huntondoom June 20, 2011, 11:46:33 PM

: [C#] line Checker
: Huntondoom June 20, 2011, 11:46:33 PM
As a practice I made a program that deletes all double lines in a file


: (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace ConsoleApplication1
{
    class Program
    {
 
        #region "processes "


        static void Main(string[] args)
        {


            if (args.Length > 0)
            {
                for (int I = 0; I < args.Length; I++)
                {
                    if (File.Exists(args[I]))
                    {
                        CheckFileLines(args[I]);
                    }
                }


            }
            else
            {
            FileInvalid:
                Console.WriteLine("Please Give Path to File To Check");
                string Path = ConvertPath(Console.ReadLine());
                if (!File.Exists(Path)) { goto FileInvalid; }
                CheckFileLines(Path);
            }
            Console.ReadLine();
        }


        static void CheckFileLines(string Path)
        {
            List<string> TextLines = new List<string>();
            List<string> LinesTNF = new List<string>();
            StreamReader Reader;


            Console.WriteLine(Path);
            try
            {
                Reader = new StreamReader(File.OpenRead(Path));
                while (!Reader.EndOfStream)
                {
                    TextLines.Add(Reader.ReadLine());                       
                }
                Reader.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("");
                Console.WriteLine(ex.Message);
            }
            for (int A = 0; A < TextLines.Count; A++)
            {
                bool isin = false;
                for (int B = 0; B < LinesTNF.Count; B++)
                {
                    if (LinesTNF[B] == TextLines[A]) { isin = true; }
                }
                if (!isin) { LinesTNF.Add(TextLines[A]); }
            }
            Console.WriteLine("Lines Deleted: " + (TextLines.Count - LinesTNF.Count).ToString());


            File.WriteAllText(Path, string.Join(Environment.NewLine, LinesTNF.ToArray()));
        }


        #endregion


        #region "Functions "


        static string ConvertPath(string Path)
        {
            Path = Path.Replace("%DOCUMENTS%", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
            return Path;
        }


        #endregion
    }
}
: Re: [C#] line Checker
: Kulverstukas June 21, 2011, 10:49:10 AM
Hmmm....
:
List := TStringList.Create;
List := dupIgnore;
List.LoadFromFile(FILE);
List.WriteToFile(FILE);

And never ever never ever ever never use "GoTo" ( { goto FileInvalid; } ). It's a bad practice. You may use it on rare occasions or when there is no other way out. You could have just made a new procedure for FileInvilid and used that, since it's an app anyway...
: Re: [C#] line Checker
: Stackprotector June 21, 2011, 11:24:27 AM
:
else
            {
            FileInvalid:
                Console.WriteLine("Please Give Path to File To Check");
                string Path = ConvertPath(Console.ReadLine());
                if (!File.Exists(Path)) { goto FileInvalid; }
                CheckFileLines(Path);
            }
This is just a primitive loop,  change it to a while loop, like while file.exists != true { do that}
: Re: [C#] line Checker
: Huntondoom June 21, 2011, 11:36:43 AM
sorry I used to my Calculator where Goto is a primary source of loop :P
: Re: [C#] line Checker
: xzid July 26, 2011, 02:54:30 AM
:
$h = @{}; gc file.txt | % { if(!$h[$_]) { $_; $h[$_] = 1 } }
I love this -> % { }, Powershell rocks!

I'd recommend using C# hashtables as well, would simplify the code(the dual for() loop part).