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 merge and save into disk.
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:
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();
}
Cannot happening merge & convert also.Please help
Muhammad Imran AnsariPosted Apr 28, 2022, 8:15 PM
Sujeet RamanPosted Apr 28, 2022, 10:38 AM
Muhammad Imran AnsariPosted Apr 28, 2022, 9:28 AM
Sujeet RamanPosted Apr 28, 2022, 8:41 AM
Muhammad Imran AnsariPosted Apr 28, 2022, 8:36 AM
Sujeet RamanPosted Apr 28, 2022, 8:25 AM
Muhammad Imran AnsariPosted Apr 28, 2022, 8:15 AM
Sujeet RamanPosted Apr 28, 2022, 7:59 AM
Header is not repeting now but its changeing the order of files in csv file.like the data order is coming like 1,3,2,5,4. How i can get the same order as in the input textfile placed?
FileInfo[] files = dir.GetFiles("unit*", SearchOption.TopDirectoryOnly);
here in FileInfo[] files, its not adding in the order from folder.I dont have any serial number for those files but need to add in the same order as in the folder.How can fix that?
Nitin SontakkePosted Apr 28, 2022, 5:15 AM
Muhammad Imran AnsariPosted Apr 28, 2022, 5:07 AM
Sujeet RamanPosted Apr 28, 2022, 4:52 AM
Muhammad Imran AnsariPosted Apr 27, 2022, 5:47 PM