Merge Multiple Text File by Using VB.NET

Posted by Stevelk under VB.NET on 9/10/2012 | Points: 10 | Views : 4863 | Status : [Member] | Replies : 1
I have a master file:
1)Master.txt

StoreCode
0001
|
|
|
|
5000

And I have up to 10 text(.txt) file:
1)T1.txt
2)T2.txt
3)T3.txt
4)T4.txt
5)T5.txt
6)T6.txt
7)T7.txt
8)T8.txt
9)T9.txt
10)T10.txt

Format:
T1.txt

StoreCode|Qty
0001|23
0002|45
0026|89
0234|100
0300|80
0560|10
1999|39
4600|20



T2.txt

StoreCode|Qty
0001|40
0003|30
0020|25
0214|10
0340|120
0760|105
3899|69
4800|20



T3.txt

StoreCode|Qty
0004|15
0007|30
0030|89
0134|100
0390|80
0660|10
2499|39
4900|20



I want the output file is:

store.csv

StoreCode,T1,T2,T3
0001,23,40
0002,45
0003,0,30
0004,0,0,15
0005
0006
0007,0,0,30
0008
|
|
0020,0,25
0021
0022
0023
0024
0025
0026,89
0030,0,0,89
|
|
0134,0,0,100
|
|
0214,0,10
|
|
0234,100
|
|
0300,80
|
|
0340,0,120
|
|
0390,0,0,80
|
|
0560,10
|
|
0660,0,0,10
|
|
0760,0,105
|
|
1999,39
|
|
2499,0,0,39
|
|
3899,69
|
|
4600,20
|
|
4800,0,20
|
|
4900,0,0,20
|
|
5000




Responses

Posted by: Megan00 on: 9/11/2012 [Member] Starter | Points: 25

Up
0
Down
You can give a try of code in below article:
http://www.codeproject.com/Articles/19856/Windows-Application-for-Merging-Text-Files

if it does not work, you also can use a convert your text to word and merge word files into one , then, extract word text, even it is not a direct method, but the code is really very simple:
save text as word:
                       

Dim mydoc As New Document()
mydoc.LoadFromFile("D:\michelle\text2doc.txt")
mydoc.SaveToFile("result0.doc", FileFormat.Doc)

merge word files:

  Dim fileName As String = OpenFile()

Dim fileMerge As String = OpenFile()
If ((Not String.IsNullOrEmpty(fileName))) AndAlso ((Not String.IsNullOrEmpty(fileMerge))) Then
'Create word document
Dim document_Renamed As New Document()
document_Renamed.LoadFromFile(fileName,FileFormat.Doc)
Dim documentMerge As New Document()
documentMerge.LoadFromFile(fileMerge, FileFormat.Doc)
For Each sec As Section In documentMerge.Sections
document_Renamed.Sections.Add(sec.Clone())
Next sec

Details of merge word files can be seen:http://everlasting129.weebly.com/1/post/2012/1/word-merge-for-c-vbnet.html
extarct word text:
        'new a stringBuilder to extract text from word document

Dim sb As New StringBuilder()

'extract text from word document
For Each section As Section In doc.Sections
For Each paragraph As Paragraph In section.Paragraphs
sb.AppendLine(paragraph.Text)
Next
Next

Method two is suitable when you can not find a direct method to merge text files with vb.net


Never give up! Smile to the world!
http://excelcsharp.blog.com/

Stevelk, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response