Click here to Skip to main content
15,888,301 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I have multiple text files in one location and they are like the name below :

A_L_PRTD_021345.txt , A_L_PRTD_432124 , like this ..

I want to merge them into single file and want to Rename the single file as L_PRTD_Currentdate>.txt

How can It be possible ? Kindly share me some cod of doing this
Posted
Comments
Karthik_Mahalingam 5-Jan-14 0:31am    
you want to append the text of each file into a single file ?
DEbopm 5-Jan-14 0:33am    
Yes ,, A_L_PRTD_021345.txt , A_L_PRTD_432124 these are my files actually I have similar type of multiple files and Those will be combined and a single file will be generated.
DEbopm 5-Jan-14 0:39am    
I have other file too in that location like A_L_UPSC_766655.txt. But Only I need to append the A_L_PRTD_ files to a single file.
Mitchell J. 5-Jan-14 0:43am    
Did you see my answer?
Karthik_Mahalingam 5-Jan-14 0:49am    
u mean, the location contains the file names starts with a_l_prt_ ...??

Use System.IO.StreamReader/StreamWriter to read/write the text files.

For example...
C#
using System.IO;

DirectoryInfo di = new DirectoryInfo("<path to containing directory>");
FileInfo[] files = di.GetFiles();

string data = "";

foreach (FileInfo fi in files)
{
    if (fi.Name.ToUpper().StartsWith("A_L_PRTD"))
    {
        StreamReader rd = new StreamReader(fi.FullName);
        data += rd.ReadToEnd();
        rd.Close();
        rd.Dispose();
        //Optionally...
        File.Delete(fi.FullName);
    }
}

StreamWriter wr = new StreamWriter(string.Format("L_PRTD_{0}.txt", DateTime.Now.ToString("ddMMyyyy")));
wr.Write(data);
wr.Close();
wr.Dispose();
 
Share this answer
 
v7
Comments
DEbopm 5-Jan-14 0:46am    
@CraigDJ .. I don't have only 2 files . I have multiple files which are starting as A_L_PRTD_<some number="">.txt and I have another files too like A_L_UPSC_<some number="">.txt. But Only I need to append the text of files like A_L_PRTD_<some number=""> in to one single combined file.
Mitchell J. 5-Jan-14 0:51am    
So you have a significant number of PRTD files mixed in with the UPSC files?
And you only want to stitch the PRTD files together?
DEbopm 5-Jan-14 0:53am    
yes . But I dont know how many PRTD files are there in that location. It is not fixed each day. Today It can be 12, Tomorrow It could be 15 , and day after tomorrow it could be only 1 ..
Mitchell J. 5-Jan-14 1:01am    
I've updated my solution.
Karthik_Mahalingam 5-Jan-14 1:03am    
5,good shortcut u have used for appending ...craigDJ .
try like this..


C#
string path = "D://Files//";
            //string[] fileNames = { "A_L_PRTD_432124.txt", "A_L_PRTD_021345.txt" };
            string fileNameStartsWIth = "A_L_PRTD_";
            string[] fileNames = Directory
                 .GetFiles(path, "*.txt", SearchOption.AllDirectories)
                 .Select(f => Path.GetFileName(f)).Where(k => k.StartsWith(fileNameStartsWIth)).ToArray();

            string finalFile = "L_PRTD_" + DateTime.Now.ToString("ddMMMyyyy") + ".txt";
            string contentSeperator = Environment.NewLine + "-----------------------------------------------------" + Environment.NewLine;
            foreach (string file in fileNames)
            {
                string content = File.ReadAllText(path + file) + contentSeperator;
                File.AppendAllText(path + finalFile, content);
            } 


reference:
File.ReadAllText [^]

File.AppendText[^]
 
Share this answer
 
v3
Comments
DEbopm 5-Jan-14 0:49am    
@KARTHIK ..
string[] fileNames = { "A_L_PRTD_432124.txt", "A_L_PRTD_021345.txt" }; This is showing that u are mentioning only for two files . But there are multiple files like the same name only the last digits are different and I dont know how many files will come . There may be 5 files and there could be 7 or rather 10. So this will not be statis as you mentioned but dynamic
Karthik_Mahalingam 5-Jan-14 0:55am    
try updated solution..
Mitchell J. 5-Jan-14 0:58am    
Nice solution :-)
Karthik_Mahalingam 5-Jan-14 1:00am    
thanks criagDJ
Karthik_Mahalingam 5-Jan-14 1:13am    
add these namespace


using System;
using System.IO;
using System.Linq;
try this
File.AppendAllText(Path, content);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900