web 2.0

Concatenate Text Files using C#

Hi,

During my last project i'm working with some SQL files, but i needed to got al of these files into one single file.

So i wrote a little program in order to contatenate all SQL files (or other text files) into one single file.

Here is the code

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

namespace ConcatFiles
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter Directory Path :");
            String filePath = Console.ReadLine();
            Console.Write("Enter Pattern (ex : *.sql) :");
            String pattern = Console.ReadLine();
            Console.Write("Enter Output FilePath :");
            String output = Console.ReadLine();

            if (String.IsNullOrEmpty(pattern))
                pattern = "*.*";

            if (!Directory.Exists(Path.GetFullPath(filePath)))
                return;

            String[] files = Directory.GetFiles(filePath, pattern);

            StringBuilder sb = new StringBuilder();
            foreach (String file in files)
            {
                sb.AppendLine();
                using (TextReader tr = new StreamReader(file))
                {
                    sb.Append(tr.ReadToEnd());
                    tr.Close();
                }
                sb.AppendLine();
            }

            // Write Output File
            using (TextWriter tw = new StreamWriter(Path.GetFullPath(output)))
            {
                tw.WriteLine(sb.ToString());
                tw.Close();
            }
        }
    }
}

Download Solution - ConcatFiles.zip

UPDATE : 23/11/2008

Someone told me in a comment that's he needs to Concatenate large files. The current implementation using StringBuilder may use a lot of RAM ..

So here is the new implementation for large files.

 

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

namespace ConcatFiles
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter Directory Path :");
            String filePath = Console.ReadLine();
            Console.Write("Enter Pattern (ex : *.sql) :");
            String pattern = Console.ReadLine();
            Console.Write("Enter Output FilePath :");
            String output = Console.ReadLine();

            if (String.IsNullOrEmpty(pattern))
                pattern = "*.*";

            if (!Directory.Exists(Path.GetFullPath(filePath)))
                return;

            String[] files = Directory.GetFiles(filePath, pattern);

            foreach (String file in files)
            {
                using (TextWriter tw = new StreamWriter(Path.GetFullPath(output), true))
                {
                    using (StreamReader tr = new StreamReader(file))
                    {
                        while (!tr.EndOfStream)
                        {
                            tw.WriteLine(tr.ReadLine());
                        }
                        tr.Close();
                    }
                    tw.Close();
                }
            }
        }
    }
}

 

Download Solution - ConcatFiles.zip

 

yield return this;



Views(3152)

kick it on DotNetKicks.com

Share/Save/Bookmark Subscribe

Currently rated 3.0 by 2 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

C#

Comments

Add comment


 

biuquote
Loading



Technorati Profile