Need help with formatting a text file in C#.net

I need help creating an executable in C#.net (Coverter.exe) that accepts a file in a particular format (Before_Format.txt) and outputs the file in another format (Formatted.txt.

So when I run converter.exe from the command prompt I should be able to pass the path where the unformatted file is located as an argument and then the executable formats the file and outputs the formatted version to a set location (path)
Can someone help me out with the code to do this in C#.net.

Please see an example of the formatted and unformatted version of the files.

formatted.txt
Before-Format.txt
wademiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kaufmed   ( ⚆ _ ⚆ )I asked the operating system what I could do to become a better programmer. It said, "Let me give you some pointers."Commented:
Add a reference to Microsoft.VisualBasicl. Then you can use the TextFieldParser:
static void Main(string[] args)
{
    if (File.Exists(args[0]))
    {
        StringBuilder temp = new StringBuilder();

        using (TextFieldParser parser = new TextFieldParser(args[0]))
        {
            parser.Delimiters = new string[] { "," };
            parser.TextFieldType = FieldType.Delimited;
            parser.HasFieldsEnclosedInQuotes = true;
            parser.TrimWhiteSpace = true;

            while (!parser.EndOfData)
            {
                DateTime tDate;
                string[] fields = parser.ReadFields();

                if (!DateTime.TryParse(fields[0], out tDate))
                {
                    continue;
                }

                temp.AppendFormat("{0:yyyyMMdd}{1}     03{2,29}{3,34}\n", tDate, fields[1], fields[3], fields[4].ToUpper());
            }
        }

        using (StreamWriter writer = new StreamWriter(args[0]))
        {
            writer.Write(temp.ToString());
        }
    }
}

Open in new window

Fernando SotoRetiredCommented:
Hi wadem;

Something like the code below should do what you need.

Fernando
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Coverter
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] pathArgs = Environment.GetCommandLineArgs();

            if (pathArgs.Length != 1)
            {
                Console.WriteLine("A path to a file to convert must ve suppied. Please try again.");
            }
            else
            {
                string path = pathArgs[1];
                ConvertFile(path);
            }
        }

        static private void ConvertFile(string path)
        {
            //File to parse Before-Format.txt full path to file
            path += @"\Before-Format.txt";

            // Get all the lines in the file
            string[] lines = File.ReadAllLines(path);

            // Where to write the file to
            string location = @"C:\Formated.txt";

            StreamWriter sw = new StreamWriter(location);
            StringBuilder sb = new StringBuilder();

            // Format the text file and write it to C:\Formated.txt
            foreach (string line in lines)
            {
                string[] fields = line.Split(new char[] { ',' });
                string dt = DateTime.Parse(fields[0]).ToString("yyyyMMdd");
                sb.Append(dt + fields[1] + "     " + "03" + "                      " );
                string amount = fields[3];
                for (int i = amount.Length; i < 38; i++) amount += " ";
                sb.Append(amount + fields[4].ToUpper());
                sw.WriteLine(sb.ToString());
                sb.Length = 0;
            }

            sw.Flush();
            sw.Close();
        }
    }
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wademiAuthor Commented:
FernandoSoto: How would i Call the program and pass the path where the file is located to be formatted and the destination path of the formatted file  from the command line interface.
Fernando SotoRetiredCommented:
Hi wademi;

To your question, "How would i Call the program and pass the path where the file is located to be formatted", from the command prompt enter the program name followed by the full path to the directory that holds the file Before-Format.txt: for example

C:\>Coverter C:\TheCompleteFilePath

To your question, "destination path of the formatted file  from the command line interface.", in your question you stated, "I should be able to pass the path where the unformatted file is located as an argument and then the executable formats the file and outputs the formatted version to a set location (path)", and therefore no parameter on the command line. To modify the output path change the following line in the code.

string location = @"C:\Fornated.txt";

To the location and filename you want the file to go.

Fernando
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.