Click here to Skip to main content
15,923,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a set of text files in one folder.
Text file Names are
DLPOR_00_01
DLPOR_00_02
DLPOR_01_01
DLPOR_01_02
SLPOR_00_01
SLPOR_00_02

Here i want to dynamically read the file from folder. And i want to merge the files depending on Firstname and middle name of the file
For Example i want to merge the DLPOR_00_01 and DLPOR_00_02 into one file because the first 2 names(DLPOR_00) same. Like this same way i want to merge the all text file.


If anyone help me for this.

Thanks.
Posted
Comments
Maciej Los 9-May-14 2:40am    
What have you tried? Where are you stuck? What kind of files do you want to merge?
vinodhini sekar 9-May-14 2:55am    
I just get the all file in fileinfo using DirectoryInfo. I explored only i need some idea for this.
DirectoryInfo dir = new DirectoryInfo("C:\\Documents and Settings\\vinodhini\\My Documents\\source folder");
FileInfo[] orig = dir.GetFiles();
FileInfo[] sort = (from file in orig orderby file.DirectoryName select file).ToArray();

All are text file.

Sample code
C#
string directory =@"D:\";
string[] fiels = System.IO.Directory.GetFiles(directory);

var fileGroups =fiels.Select(f=>System.IO.Path.GetFileName(f))
       .Where(f=>f.Split('_').Length==3)
       .GroupBy(f=> f.Substring(0,f.LastIndexOf('_'))).ToList();
       
foreach(var g in fileGroups)
{
    string newFileName = g.Key+".txt";
    string newfileContent ="";
    foreach( var f in g)
    {
      newfileContent +=
              System.IO.File.ReadAllText(System.IO.Path.Combine(directory, f));
    }
    System.IO.File.WriteAllText(System.IO.Path.Combine(directory, newFileName), 
                                   newfileContent);
}


I have used several LINQ and System.IO methods, if you you not understand anything please ask.

1. I'm taking all the files in the directory using Directory.GetFiles method
2. then filder and get only the files with the format xxxx_xxxx_xx Using LiNQ and then group the result based on the file name without final number.
3. loop the result and create new file with the content of grouped files.
 
Share this answer
 
v2
Comments
vinodhini sekar 9-May-14 3:22am    
Hi DamithSL,
Thanks it works well.
How can i save this new file in another folder?
DamithSL 9-May-14 3:25am    
when you call WriteAllText, give directory as another directory
vinodhini sekar 9-May-14 3:30am    
Yes i tried that... Now it works... Thank you
vinodhini sekar 9-May-14 3:32am    
Hi DanithSL,
Sorry to disturb you. I have one explanation.please explain the following line.
var fileGroups =fiels.Select(f=>System.IO.Path.GetFileName(f))
.Where(f=>f.Split('_').Length==3)
.GroupBy(f=> f.Substring(0,f.LastIndexOf('_'))).ToList();
vinodhini sekar 9-May-14 3:37am    
Here after merging i want to move the text files which we used to merge into another folder with _complete name.Is this possible?
You need to loop through the collection of files in the folder, then compare their names and merge them in case when their names fulfill the criteria. Use List<String>[^] general class, because it enables to sort, search and concat data.

Here is an example: How to: Enumerate Directories and Files[^]
 
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