I am writing a program that opens up one file at a time, parses it, and outputs the parsed data into a new .txt document that is named based off of the file coming in. There are over 50 files that are going to be read and parsed.
So if the opened file was named something like this: STACK-OVERFLOW-125663-D2.txt, the output file would be something like this 125663-D2.txt.
Each time a file is read, it is parsed for their part numbers. Each file will contain lines similar to this (The 8th seperated comma values (ie, 119082, 119083, 119040, 119085, 119084) are the part number values.):
"00003",6,"D","C20",-70.10,42.06,90.00,"119082",0,1,2,0,0,"",0,"001"
"00004",6,"D","C21",-67.91,42.06,90.00,"119082",0,1,2,0,0,"",0,"001"
"00005",13,"D","C23",-66.91,59.07,180.00,"119083",0,1,2,0,0,"",0,"002"
"00006",13,"D","R10",-77.32,66.88,90.00,"119040",0,1,2,0,0,"",0,"003"
"00007",13,"D","L3",-77.64,77.48,90.00,"119085",0,1,2,0,0,"",0,"004"
"00008",20,"D","D1",-62.91,103.77,0.00,"119084",0,1,2,0,0,"",0,"005"
"00009",21,"D","D1",-25.83,103.77,0.00,"119084",0,1,2,0,0,"",0,"005"
"00010",14,"D","L3",-40.56,77.48,90.00,"119085",0,1,2,0,0,"",0,"004"
"00011",14,"D","R10",-40.24,66.88,90.00,"119040",0,1,2,0,0,"",0,"003"
Now what I need to do is check another .txt file.. let’s say it is called “DATABASE.txt” to see if these part number exist in there already. This data base file will look something like this:
119082: 125663-D2, 123456-A1,
119083: 125663-D2,
119085: 125663-D2, 123456-A1, 987654321-Z11234, 1111111-B50
So, in the DATABASE.txt file and the file opened above, I want to check all of the part numbers from the opened file and see if they exist in the data base.
-
If the part does exist, I want to concat the filename (the output file) to the end of the line that the part number was found on.
-
If the part does not exist, I want to add the part to the file and sort the file using
list.Sort().
I am unsure on how to do this, can anyone help?
Here is some of my code so far:
List<string> partNumberLines = new List<string>();
string file = openFile.FileName;
string splitFile = file.Split('\\');
string[] savedName = splitFile[splitFile.Length - 1].Split('.');
string[] lineNumber = savedNamed[2].Split('-');
string fileName = savedNamed[1] + "-" + lineNumber[0] + ".txt";
foreach (string line in fileList)
{
string[] splitLine = line.Split('\n');
for (int i = 0; i < splitLine.Length; i++)
{
string tempSplit = splitLine[i].Split(','); // splits each line by commas
if (tempSplit.Length.Equals(16))
{
tempSplit[7] = tempSplit[7].TrimStart('"'); //trims the quotes from the part numbers
tempSplit[7] = tempSplit[7].TrimEnd('"');
}
}
}
partNumberLines = partNumberLines.Distinct().ToList(); //gets rid of duplicate partnumbers in one file.
So my code is getting all of the part numbers and the name of the file.. I just do not know how to open up an existing file (and if it does not exist, create it) and search through the file and look for matches in the List: partNumberLines. And if it matches, concat the file name to the current line. If it does not match, create a new line and add the part number and file name and then sort the file numerically by part number.
Can anyone help me figure this out?
Hope you haven’t given up. Here’s a sample class. I’ve completed it from my last post. Save your database data above to
database.txtand your parts data toparts.txtand modify the paths to see how it works. Hope it helps you. If you have any more questions, feel free to ask.Edit
If you want a static database and you want to allow the user to choose a parts file, then you could do this in a button click event: