C# Text File Input Multi-File Output
Asked Answered
W

4

6

I have a file that looks something like this:

|29923C|SomeGuy,NameHere1     |00039252|042311|Some Address Info Here |
|47422K|SomeGuy,NameHere2     |00039252|042311|Some Address Info Here |
|98753D|SomeGuy,NameHere3     |00039252|042311|Some Address Info Here |
|29923C|SomeGuy,NameHere4     |00039252|042311|Some Address Info Here |
|47422K|SomeGuy,NameHere5     |00039252|042311|Some Address Info Here |

I need to break the file up into multiple files based on the first 6 characters starting in position 2.

File 1 named 29923c.asc:

|29923C|SomeGuy,NameHere1     |00039252|042311|Some Address Info Here |
|29923C|SomeGuy,NameHere4     |00039252|042311|Some Address Info Here |

File 2 named 47422K.asc:

|47422K|SomeGuy,NameHere5     |00039252|042311|Some Address Info Here |
|47422K|SomeGuy,NameHere2     |00039252|042311|Some Address Info Here |

File 3 named 9875D.asc:

|98753D|SomeGuy,NameHere3     |00039252|042311|Some Address Info Here |

I don't know what will be in the file before the program gets it, just the format. The 6 digits will change depending on the customer. I don't know what they will be.

The only thing I know is the format.

Can anyone give me a suggestion as to how to dynamically obtain\maintain this information so that I can parse it out into individual files?

Whitleywhitlock answered 21/5, 2011 at 16:45 Comment(1)
Wow I feel like a fool. I was other thinking this way to much lol. Thanks for the info! Everyone's answer will work and I appreciate the help!Whitleywhitlock
M
1

Read line by line. Get code from each line and create file, put reference to opened file stream to dictionary with code as a key. On each next line check dictionary for the key and use opened stream or create new one. After reading all file close all streams.

This algorithm will prevent from using too much memory for a file lines if it has a big size.

For parsing each line you can simply use RegEx, for example.

Mongolic answered 21/5, 2011 at 16:51 Comment(1)
This was the simplest way to handle it. I read a line and then output it to the file that it needed to be in. Every file that I output to, I added to a dictionary so that I knew what I had to handle once was done.Whitleywhitlock
S
4

I suggest using a parser such a the TextFieldParser class.

You could read the data into memory, sort it using the first field then write out individual files.

Stereotomy answered 21/5, 2011 at 16:48 Comment(0)
T
2
List<string> lines ; // load lines form file

Dictionary<string,List<string>> dic = new Dictionary<string,List<string>>();
foreach(string line in lines) 
{
    string key = line.Split('|')[0];

    if(!dic.ContainsKey(key))
        dic.Add(key,new List<string>{line});
    else 
        dic[key].Add(line) 
}

foreach(var pair in dic) 
{
    //create file and store there pair.Value   
}
Thermic answered 21/5, 2011 at 16:53 Comment(3)
What will be if a file has a very big size? All stored to memory?Mongolic
@archer ErocM didn't specify that file is very hugeThermic
At the most, it could be a few hundred lines.Whitleywhitlock
M
1

Read line by line. Get code from each line and create file, put reference to opened file stream to dictionary with code as a key. On each next line check dictionary for the key and use opened stream or create new one. After reading all file close all streams.

This algorithm will prevent from using too much memory for a file lines if it has a big size.

For parsing each line you can simply use RegEx, for example.

Mongolic answered 21/5, 2011 at 16:51 Comment(1)
This was the simplest way to handle it. I read a line and then output it to the file that it needed to be in. Every file that I output to, I added to a dictionary so that I knew what I had to handle once was done.Whitleywhitlock
D
0

You could do it the brute-force way.

Reading:

Dictionary<string, List<string>> DICT;
Until End of File {
   Read a line to LINE
   Read characters 1-7 in LINE to CUSTOMERID
   DICT[CUSTOMERID].Add(LINE);
}

Writing:

foreach KeyValuePair entry in DICT {
   Create file with name entry.Key
   foreach string line in entry.Value {
      Write line to file line
   }
}
Dimitrovo answered 21/5, 2011 at 17:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.