|
-
Nov 18th, 2022, 02:03 AM
#1
Thread Starter
Addicted Member
[RESOLVED] VB.NET Combine .txt files without header
Hi all
I have the following code to combine all .txt files in a folder into a single .txt file :
Code:
Dim path As String = "D:\Wingerdbou\Lutzville2\InvoerDataFiles\Ander Organisasies\Wesbank\HistorieseFiles\"
Dim topath As String = "D:\Wingerdbou\Lutzville2\InvoerDataFiles\Ander Organisasies\Wesbank\HistorieseFiles\"
Dim files As String = "*.txt"
Dim txtFiles() As String
txtFiles = Directory.GetFiles(path, files)
Using writer As New StreamWriter(topath & "\Combined.txt")
For i As Integer = 0 To txtFiles.Length - 1
Using reader As StreamReader = File.OpenText(txtFiles(i))
writer.Write(reader.ReadToEnd())
End Using
Next i
End Using
However in the process I also combine all the headers in the first line of all the files into the new .txt file.
I just cannot figure out how to use only one column header. The data structure of the files are identical.
Any help would be appreciated.
Regards
-
Nov 18th, 2022, 02:22 AM
#2
Re: VB.NET Combine .txt files without header
vb.net Code:
Dim sourceFolderPath = "source folder path here"
Dim outputFilPath = "output file path here"
Dim sourceFilePaths = Directory.GetFiles(sourceFolderPath, "*.txt")
Using writer As New StreamWriter(outputFilPath)
For i = 0 To sourceFilePaths.GetUpperBound(0)
Dim lines = File.ReadLines(sourceFilePaths(i))
'Skip the first line if this is not the first file.
If i > 0 Then
lines = lines.Skip(1)
End If
For Each line In lines
writer.WriteLine(line)
Next
Next
End Using
-
Nov 18th, 2022, 07:47 AM
#3
Thread Starter
Addicted Member
Re: VB.NET Combine .txt files without header
Thank you jmcilhinney.
Works like a charm.
However the new file in in Windows(CRLF) UTF8 format. The original files are in Unix(LF) UTF8 format.
For importing it into MYSQL, I changed the carriage return to \r\n instead of \n (which works Unix(LF) files).
I have tried both \r\n and \n , but it does not import with no error messages.
Any Ideas?
-
Nov 18th, 2022, 07:58 AM
#4
Re: VB.NET Combine .txt files without header
You could change this:
to this:
vb.net Code:
writer.Write(line & ControlChars.Lf)
-
Nov 19th, 2022, 01:04 AM
#5
Thread Starter
Addicted Member
Re: VB.NET Combine .txt files without header
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|