Require help in a use case, below is the code of C# which merges data from multiple CSV files, in the following code I also want a condition that, a column with heading FileName should be added in the file at index 0 and the filename of which data is being merged that filename should be printed in that column. Can anybody please guide.
var allCsv = Directory.EnumerateFiles(@"C:\Users\Documents\UiPath\Files1", "*.csv", SearchOption.TopDirectoryOnly);
string[] header =
{
File.ReadLines(allCsv.First()).First(l => !string.IsNullOrWhiteSpace(l))
};
// Get CSV Data
var mergedData = allCsv.SelectMany(csv => File.ReadLines(csv).SkipWhile(l => string.IsNullOrWhiteSpace(l)).Skip(1));
// skip header of each file
File.WriteAllLines(@"C:\Users\Documents\UiPath\test.csv", header.Concat(mergedData));
Very apologies for this much delay, my system got broke down thus i was not able to communicate. Can you please help me now with some solution.
I am providing the examples
Input - I have uploaded the files in Zip format as the platform was not allowing me to upload .csv files, please extract the zip you will get 3 .csv input files SampleInput1.zip (637 Bytes)
Output - As described above in the output file extra column is added at index 0 with names of file from where the data is taken. SampleOutput.xlsx (8.6 KB)
I tried you code it is working thankyou, but in the first column the whole path of the file is coming with the name can you please help as I only need the file name and not the complete path.
Thanks a lot @ptrobot for your solutions, I had one more question if the files are similar but in .xlsx format will this code work if not then what do i need to do to make it working.
If one of the csv file gives any exception then the process will halt, can you please help in this code with some try catch activity as well so as it can go to the next file and print or capture the file names with exception in any collection object?
@ptrobot@Yoichi I cannot mark 2 solutions in this forum so bad about that. Thanks a lot for sharing your knowledge.
I will open a new thread for my other questions in this code