Merge Multiple text file
Merge Multiple text file
Merge Multiple text file
Merge Multiple text file
Merge Multiple text file
Merge Multiple text file Merge Multiple text file Merge Multiple text file Merge Multiple text file Merge Multiple text file Merge Multiple text file Merge Multiple text file Merge Multiple text file
Merge Multiple text file Merge Multiple text file
Merge Multiple text file
Go Back  Xtreme Visual Basic Talk > > > Merge Multiple text file


Reply
 
Thread Tools Display Modes
  #1  
Old 09-10-2012, 02:52 AM
stevelk stevelk is offline
Newcomer
 
Join Date: Sep 2012
Posts: 1
Default Merge Multiple text file


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
Reply With Quote
  #2  
Old 09-10-2012, 08:56 AM
Gruff's Avatar
GruffMerge Multiple text file Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

Welcome to the forum steve. Do not forget to read the forum Posting Guidelines.

If your file sizes are not huge then the easiest method would be to read all your files into one list(of string) variable. Sort the entire list such that all "store codes" are adjacent

1000|33
1000|72
1000|11
1003|80
1003|85
...

Then make one pass through the list accumulating each "store codes" "Qty"
Output to your final file each time the "store codes" changes.
You will need a final forced output at the end of the list.

This is a common programming task and VB.Net makes it easy.

Not sure why you would need your proposed initial master file at all unless it allows you to skip given "store codes"

Also I assume you do not really want braces output. ( [ ] ) .
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 09-10-2012 at 09:06 AM.
Reply With Quote
  #3  
Old 09-10-2012, 09:28 AM
AtmaWeapon's Avatar
AtmaWeaponMerge Multiple text file AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

I don't see a question in there, so it looks suspiciously like "Who can do it for me?" We don't really do that here. But let's talk about it.

For the basics, you might want to review File I/O in VB .NET. It's a little more detailed than you usually need, but knowing those details is what helps you figure out how to solve problems.

Aside from that, here's some pseudocode and thoughts about what I think a solution will look like.

I'm not sure you need to fool with the Master.txt file at all. You can build a list of store codes as you parse the others, and you don't exactly need help prepopulating a list of 1-5000. I'm also curious why the output format only includes T1, T2, and T3. I'm assuming it's for brevity. If the Tx.txt files are sorted, that gets even easier. But let's say they're not. It's still easy.

First, consider what a line of output looks like in the final file. A class to represent that would be helpful:
Code:
Class StoreInfo
    Public Property StoreCode As String
    Public Property FileValues As String()
End Class
You could opt for Integer as the data types. You could also opt for an array instead of the properties. But it'll cause some problems later. Let's just deal with it as-is for now.

With that in place, a Dictionary(Of Integer, StoreInfo) will let you keep track of objects as you walk through them. Here's how I'd read T1.txt through T10.txt:
Code:
* Create the Dictionary
* For Each file:
    * Open the file
    * Skip the header line
    * For each line in the file:
        * Read the line.
        * The portion before the "|" character is a store code.
        * The portion after the "|" character is a quantity.
        * Find the StoreInfo for this store code in the dictionary. If it hasn't been created, create it.
        * Add the quantity to the right array element.
That gives you a nice list of StoreInfo objects that will have either a quantity or Nothing in its array elements for quantities. This raises a problem not apparent in your format: What do you do if there's no quantity for a file? I'm assuming it just leaves a blank entry. Suppose store 23 had entries in only T1 and T3; it'd look like this:
Code:
0023,42,,23
I'm also assuming the "|" in the final result means "nothing for this store". If you have different requirements, you'll have to implement them. This is what that'd look like:
Code:
* For Each valid store code (likely 1-5000):
    * If the store is not in the dictionary:
        * Output "|"
    * Else:
        * Output the store code.
        * For each element in the quantity array:
            * If the value is Nothing:
                * Output ","
            * Else:
                * Output the value followed by a comma.
Give it a try, and feel free to ask questions about parts that are unclear. We learn by doing.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #4  
Old 09-10-2012, 12:15 PM
passel's Avatar
passelMerge Multiple text file passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,026
Default

Not a big deal, but his desired example does show that he puts "leading 0's" if a subsequent file had a quantity, but an earlier file didn't.
Code:
0002,45         'T1 had 45, but T2 and T3 had nothing for that store
0003,0,30       'T2 had 30, but T1 and T3 had nothing for that store
0004,0,0,15     'T3 had 15, but T1 and T2 had nothing for that store  
0005            'None of the files had anything for store 0005
Makes things a bit more complicated.
I was thinking along the lines of AtmaWeapon, so expected empty fields ,, in the output, but saw that you had 0's, but only where a subsequent file had a quantity.

It would be simpler if you had a place holder for each input file on a line so you always had the same number of fields per line, whether they were empty ,, or set to 0 ,0, .

But if your parser is already set to expect to only have to parse the minimum number of necessary fields (i.e. no trailing empty fields), then that will have to be worked out a bit different from the ideas so far.
Let us know how you make out.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Merge Multiple text file
Merge Multiple text file
Merge Multiple text file Merge Multiple text file
Merge Multiple text file
Merge Multiple text file
Merge Multiple text file Merge Multiple text file Merge Multiple text file Merge Multiple text file Merge Multiple text file Merge Multiple text file Merge Multiple text file
Merge Multiple text file
Merge Multiple text file
 
Merge Multiple text file
Merge Multiple text file
 
-->