Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6586409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:47:14+00:00 2026-05-25T16:47:14+00:00

I am writing a program that opens up one file at a time, parses

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:47:14+00:00Added an answer on May 25, 2026 at 4:47 pm

    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.txt and your parts data to parts.txt and modify the paths to see how it works. Hope it helps you. If you have any more questions, feel free to ask.

    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            private class DataBaseRecord
            {
                public string PartNumber { get; set; }
                public List<string> FileNames { get; set; }
                public DataBaseRecord(string _PartNumber, List<string> _FileNames)
                {
                    PartNumber = _PartNumber;
                    FileNames = _FileNames;
                }
            }
    
            private class DataBase
            {
                public string databaseFile { get; set; }
                List<DataBaseRecord> records;
                public DataBase(string _databaseFile)
                {
                    databaseFile = _databaseFile;
                    records = new List<DataBaseRecord>();
                }
                public void AddRecord(string partNumber, string fileName)
                {
                    if (string.IsNullOrWhiteSpace(partNumber))
                        return;
    
                    if (string.IsNullOrWhiteSpace(fileName))
                        return;
    
                    bool exists = records.Count(x => x.PartNumber == partNumber) > 0;
                    if (!exists)
                    {
                        records.Add(new DataBaseRecord(partNumber, new List<string>() { fileName }));
                    }
                    else
                    {
                        var record = from x in records where x.PartNumber == partNumber select x;
                        foreach (DataBaseRecord dbr in record)
                        {
                            exists = dbr.FileNames.Count(x => x == fileName) > 0;
                            if (!exists)
                                dbr.FileNames.Add(fileName);
                        }
                    }
                }
                public void Read()
                {
                    // read all database records into data structure
                    using (StreamReader sr = new StreamReader(databaseFile))
                    {
                        while (!sr.EndOfStream)
                        {
                            string line = sr.ReadLine();
                            string partNumber = line.Split(':')[0].Trim();
                            if (partNumber[0] == '\"')
                                partNumber = partNumber.Substring(1, partNumber.Length - 2);
                            string[] files = line.Split(new string[]{":"}, StringSplitOptions.None)[1].Split(new string[]{","}, StringSplitOptions.RemoveEmptyEntries);
                            List<string> fileNumbers = new List<string>();
                            foreach (String file in files)
                            {
                                if (!string.IsNullOrWhiteSpace(file))
                                {
                                    fileNumbers.Add(file.Trim());
                                }
                            }
                            records.Add(new DataBaseRecord(partNumber, fileNumbers));
                        }
                    }
                }
                public void Write()
                {
                    // write out database using the records
                    var sortedRecords = from x in records orderby x.PartNumber select x;
                    using (StreamWriter sw = new StreamWriter(databaseFile))
                    {
                        foreach (DataBaseRecord record in sortedRecords)
                        {
                            string line = record.PartNumber + ": ";
                            for (int index = 0; index < record.FileNames.Count; index++)
                            {
                                line += record.FileNames[index];
                                if (index < record.FileNames.Count - 1)
                                    line += ", ";
                            }
                            sw.WriteLine(line);
                        }
                    }
                }
            }
    
            static void Main(string[] args)
            {
                // replace with name of your database
                DataBase db = new DataBase(@"C:\Users\jondoe\Desktop\DataBase.txt");
                db.Read();
    
                // replace with list of your parts files
                string[] partsFiles = new string[] { @"C:\Users\jondoe\Desktop\parts.txt" };
                foreach (string partsFile in partsFiles)
                {
                    using (StreamReader sr = new StreamReader(partsFile))
                    {
                        while (!sr.EndOfStream)
                        {
                            string line = sr.ReadLine();
                            string partNumber = line.Split(new string[] { "," }, StringSplitOptions.None)[7];
                            if (partNumber[0] == '\"')
                                partNumber = partNumber.Substring(1, partNumber.Length - 2);
                            db.AddRecord(partNumber, Path.GetFileNameWithoutExtension(partsFile));
                        }
                    }                
                }
    
                db.Write();
            }
        }
    }
    

    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:

    private void btnOpenFile_Click(object sender, EventArgs e)
    {
        DataBase db = new DataBase(@"C:\Users\JonDoe\Desktop\DataBase.txt");
        db.Read();
    
        using (OpenFileDialog ofd = new OpenFileDialog())
        {
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                using (StreamReader sr = new StreamReader(ofd.FileName))
                {
                    while (!sr.EndOfStream)
                    {
                        string line = sr.ReadLine();
                        string partNumber = line.Split(new string[] { "," }, StringSplitOptions.None)[7];
                        if (partNumber[0] == '\"')
                            partNumber = partNumber.Substring(1, partNumber.Length - 2);
                        db.AddRecord(partNumber, Path.GetFileNameWithoutExtension(ofd.FileName));
                    }
                }
                db.Write();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a program that gathers user input for two file names, and opens
I am writing a java program that needs a file open dialog. The file
I'm writing a program that requires input in the form of a document, it
Im writing a program that should read input via stdin, so I have the
I'm writing a program that sends an email out at a client's specific local
If you are writing a program that is executable from the command line, you
I'm writing a program that uses SetWindowRgn to make transparent holes in a window
I'm writing a program that contains a generational garbage collector. There are just two
I am writing a program that will draw a solid along the curve of
I am writing a program that does a lot of writes to a Postgres

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.