הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Sunday, August 23, 2020 5:17 PM
I want merge multiple csv files with same layout from the same folder
example :
csv1.csv
ID, PINA,PINB,PCS
1,100,200,450
2,99,285,300
csv1.csv
ID, PINA,PINB,PCS
1,100,200,999
2,99,285,998
out.csv (The file I want make by VB.net)
ID, PINA,PINB,PCS,PCS
1,100,200,450,999
2,99,285,300,998
my problem code :
Dim FileReader As StreamReader
Dim i As Integer = 0
Dim temp As String
For i = 0 To LstFiles.Items.Count - 1
FileReader = File.OpenText(LstFiles.Items.Item(i))
temp = FileReader.ReadToEnd
File.AppendAllText(SaveFileDialog1.FileName, temp)
Next
Please guide me.
Thanks a lot !
All replies (10)
Sunday, August 23, 2020 6:29 PM ✅Answered | 1 vote
Hi
Based on the limited information you provided, here is some code you can try out.
I made a folder caled TEST on my Desktop and placed 4 test files into it.named CSV1.csv, CSV2.csv, CSV3.csv and CSV4.csv.
Runnnig the code produced a file called OUTcsv.csv on my Desktop which merged all the 4 test files following the pattern you posted.
Try this code out (with test data only) and see if it fits with your requirements.
This is a stand alone example, no UI is needed, but you may need to adjust the paths in this example to suit your setup.
Option Strict On
Option Explicit On
Public Class Form1
Dim INpath As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "TEST")
Dim OUTpath As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "OUTcsv.csv")
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim infiles As List(Of String) = IO.Directory.GetFiles(INpath, "*.csv").ToList
Dim outfirst As List(Of String) = IO.File.ReadAllLines(infiles.Item(0)).ToList
infiles.RemoveAt(0)
For Each s As String In infiles
Dim c As Integer = 0
For Each line As String In IO.File.ReadAllLines(s)
Dim sp() As String = Split(line, ","c)
outfirst.Item(c) &= "," & sp(sp.Length - 1)
c += 1
Next
Next
IO.File.WriteAllLines(OUTpath, outfirst)
End
End Sub
End Class
Regards Les, Livingston, Scotland
Sunday, September 13, 2020 2:44 PM ✅Answered
Hi
Sorry, I just can't make sense of your image(s).
Lets try and get onto the same page. It looks like you are trying to use my code on your own files and that is NOT a valid thing to try at this stage.
As per the start of this thread.
Please create a test folder on your Desktop named TEST
Into that folder put the following files
CSV1.csv
1,100,200,450
2,99,285,300
CSV2.csv
1,100,200,999
2,99,285,998
CSV3.csv
1,100,200,888
2,99,285,666
CSV4.csv
1,100,200,777
2,99,285,444
Use the code I posted (ONLY that code) and run it. See the Output file called
OUTcsv.csv which should now be on your Desktop.
Do the results match your original post requirements?
Do the results match your actual requirements?
Regards Les, Livingston, Scotland
Sunday, August 23, 2020 5:54 PM
Hi
Please state the 'rules' that are to be used.
For example, looking at your post,
1. take the first line of first file (ID, PINA,PINB,PCS)
2. take the last item from the first line of the second file (PCS)
3. append 2) to 1) above comma separated giving (ID, PINA,PINB,PCS,PCS)
4. that is the first line of output file
5 repeat with each of the remaining lines following same pattern as per 1)-4) above
*
Now, are all the files exactly the same format as your examples?
For subsequent files, is it the same pattern as 1)-4) above - i.e. if third file first line had (say)
ID, PINA,PINB,PCS,XYZ - would the XYZ be appended to the output file as (ID, PINA,PINB,PCS,PCS,XYZ) - and similar for ALL files - ALL lines.
I would assume all files have the same number of lines - is that correct?
*
I note also that there is a leading space in the PINA item in all 3 files - is that correct?
I note also that you have stipulated the same file name as CSV1.csv for both input files - are they from different Paths?
*
You really should explain things without someone having to ask!
Regards Les, Livingston, Scotland
Wednesday, September 2, 2020 5:50 AM
Hi thuan2210,
How is the question going? Did you solve your problem? If your question has been answered then please click the "Mark as Answer" Link at the bottom of the correct post(s), so that it will help other members to find the solution quickly if they face a similar issue.
Best Regards,
Xingyu Zhao
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Sunday, September 13, 2020 11:29 AM
I'm sorry for the late response.
I'll make a more explicit request with the example files.
My example files (files on my OneDrive): https://1drv.ms/u/s!Arcw6GDhmLErifQ9rae2sHVn7ioQeg?e=zp3A7x
thanks for your interrest in my question.
Sunday, September 13, 2020 12:27 PM
Hi
Well, after providing a best guess example, you choose to ignore it (or at least not provide any feedback on it). Did you even try it out? I had also asked some questions which you have ignored. Instead, you have poorly reworded your question which would lead to many more questions to you. Since you have elected to ignore my earlier contribution, I hereby choose to ignore your latest post.
Good luck.
Regards Les, Livingston, Scotland
Sunday, September 13, 2020 12:56 PM
Hi
Well, after providing a best guess example, you choose to ignore it (or at least not provide any feedback on it). Did you even try it out? I had also asked some questions which you have ignored. Instead, you have poorly reworded your question which would lead to many more questions to you. Since you have elected to ignore my earlier contribution, I hereby choose to ignore your latest post.
Good luck.
Regards Les, Livingston, Scotland
Hi Leshay.
I think all the questions you need I have provided with pictures and actual files.
I'm very sorry for letting you think that way.
Sorry for my little English to explain more fully"Did you even try it out?" : I really tried everything but it didn't come out as expected. (I started learning about VB.NET 2 months ago)
once again, I'm very sorry for letting you think that way.
Sunday, September 13, 2020 1:16 PM
Hi
OK, at least you replied to my post.
You say that 'didn't come out as expected.' when you tried the code I posted. Please explain what that means? Did it run OK but produced the wrong results? Did it not run at all? Did it run but fail with errors? etc etc etc
As you can see, you need to be reasonable specific when you state something.
Back to square 1 - we will need to get my first example to work - even if it doesn't produce what you want initially. So, please explain what happens when you try the example (You did set uo a test folder with some test files in it - didn't you?)
Regards Les, Livingston, Scotland
Sunday, September 13, 2020 2:22 PM
the code U posted : I test by put 2 files in TEST foler but starting form file no.2 only shows the last column of file no.2
Dim infiles As List(Of String) = IO.Directory.GetFiles(INpath, "*.csv").ToList
Dim outfirst As List(Of String) = IO.File.ReadAllLines(infiles.Item(0)).ToList
infiles.RemoveAt(0)
For Each s As String In infiles
Dim c As Integer = 0
For Each line As String In IO.File.ReadAllLines(s)
Dim sp() As String = Split(line, ","c)
outfirst.Item(c) &= "," & sp(sp.Length - 1)
c += 1
Next
Next
HERE ARE THE RESULTS :
Sunday, September 13, 2020 3:28 PM
YES. WITH FILE STRUCTURE LIKE THAT CODE WORKS OK.THANK YOU FOR HELPING!