Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am implimenting some functionality but I am stuck on some logic..All documentation related to multiple text file merge is related to save that to folder.but i dont want to save that after merge.i need to convert to csv and merge.like below code merging and save to folder
C#
using (var output = new StreamWriter("D:\\TMP\\output"))
{
  foreach (var file in Directory.GetFiles("D:\\TMP", "*.*"))
  {
    using (var input = new StreamReader(file))
    {
      output.WriteLine(input.ReadToEnd());
    }
  }
}

here are my requirements..

1.want to search some directory and get specific named .txt files(getting files by partial names)

2.I want to merge these multiple text files in to one text file

(remove repeating heading from each txt file ie: only one heading is needed.In all txt files headings are same but i need to avoid duplicate heading)

3.this merged txt file i want to convert in to .csv file and save in to my destination path.This csv file would be the end result

What I have tried:

C#
string folderPath = @"C:/Temp/";
string destfile=@"C:/CSVFOLDER/sample.csv";
  StreamWriter csvfile;

            string[] lines, cells;

            

DirectoryInfo dir= new DirectoryInfo(folderPath);
FileInfo[] files = dir.GetFiles("unit*", SearchOption.TopDirectoryOnly); //txt file starting with unit i have to take from folder

foreach (var item in files)
{
	
	
    lines = File.ReadAllLines(file); //how to fix string issue here?-i cant do this with file type

            csvfile = new StreamWriter(destfile);

            for (int x = 0; x < lines.Length; x++)

            {

                cells = lines[x].Split(new Char[] { '\t', ';' });

                for (int j = 0; j < cells.Length; j++)

                    csvfile.Write(cells[j] + "\t");

                csvfile.WriteLine();

            }

            csvfile.Close();
}
Posted
Updated 27-Apr-22 13:47pm
v4
Comments
[no name] 27-Apr-22 11:37am    
"var item" is a FileInfo; use .Name to get the file name.

https://docs.microsoft.com/en-us/dotnet/api/system.io.fileinfo.name?view=net-6.0

1 solution

On first point, Use file fullName instead of file object like:
C#
lines = File.ReadAllLines(file.FullName);

On second point, initialize csvfile outside the foreach loop because this object initialized every time and you are not getting merged result. Full code is below:
C#
string folderPath = @"C:/Temp/";
string destfile = @"C:/CSVFOLDER/sample.csv";
StreamWriter csvfile;

string[] lines, cells;

DirectoryInfo dir = new DirectoryInfo(folderPath);
FileInfo[] files = dir.GetFiles("unit*", SearchOption.TopDirectoryOnly); //txt file starting with unit i have to take from folder
csvfile = new StreamWriter(destfile);

foreach (var file in files)
{
	lines = File.ReadAllLines(file.FullName); //how to fix string issue here?-i cant do this with file type
	for (int x = 0; x < lines.Length; x++)
	{
		cells = lines[x].Split(new Char[] { '\t', ';' });
		for (int j = 0; j < cells.Length; j++)
			csvfile.Write(cells[j] + "\t");
		csvfile.WriteLine();
	}
}
csvfile.Close();
 
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