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 808801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:37:21+00:00 2026-05-15T00:37:21+00:00

Thanks to Code Poet, I am now working off of this code to parse

  • 0

Thanks to Code Poet, I am now working off of this code to parse all .txt files in a directory and store them in a database. I need a bit more help though… The file names are R303717COMP_148A2075_20100520.txt (the middle section is unique per file). I would like to add something to code so that it can parse out the R303717COMP and put that in the left column of the database such as: (this is not the only R number we have)

R303717COMP  data  data  data
R303717COMP  data  data  data
R303717COMP  data  data  data
etc

Lastly, I would like to have it store each full file name into another table that gets checked so that it doesn’t get processed twice.. Any Help is appreciated.

using System; 
using System.Data; 
using System.Data.SQLite; 
using System.IO; 

namespace CSVImport 
{ 
    internal class Program 
    { 
        private static void Main(string[] args) 
        { 
            using (SQLiteConnection con = new SQLiteConnection("data source=data.db3")) 
            { 
                if (!File.Exists("data.db3")) 
                { 
                    con.Open(); 
                    using (SQLiteCommand cmd = con.CreateCommand()) 
                    { 
                        cmd.CommandText = 
                            @" 
                        CREATE TABLE [Import] ( 
                            [RowId] integer PRIMARY KEY AUTOINCREMENT NOT NULL, 
                            [FeatType] varchar, 
                            [FeatName] varchar, 
                            [Value] varchar, 
                            [Actual] decimal, 
                            [Nominal] decimal, 
                            [Dev] decimal, 
                            [TolMin] decimal, 
                            [TolPlus] decimal, 
                            [OutOfTol] decimal, 
                            [Comment] nvarchar);"; 
                        cmd.ExecuteNonQuery(); 
                    } 
                    con.Close(); 
                } 

                con.Open(); 

                using (SQLiteCommand insertCommand = con.CreateCommand()) 
                { 
                    insertCommand.CommandText = 
                        @" 
                    INSERT INTO Import  (FeatType, FeatName, Value, Actual, Nominal, Dev, TolMin, TolPlus, OutOfTol, Comment) 
                    VALUES     (@FeatType, @FeatName, @Value, @Actual, @Nominal, @Dev, @TolMin, @TolPlus, @OutOfTol, @Comment);"; 

                    insertCommand.Parameters.Add(new SQLiteParameter("@FeatType", DbType.String)); 
                    insertCommand.Parameters.Add(new SQLiteParameter("@FeatName", DbType.String)); 
                    insertCommand.Parameters.Add(new SQLiteParameter("@Value", DbType.String)); 
                    insertCommand.Parameters.Add(new SQLiteParameter("@Actual", DbType.Decimal)); 
                    insertCommand.Parameters.Add(new SQLiteParameter("@Nominal", DbType.Decimal)); 
                    insertCommand.Parameters.Add(new SQLiteParameter("@Dev", DbType.Decimal)); 
                    insertCommand.Parameters.Add(new SQLiteParameter("@TolMin", DbType.Decimal)); 
                    insertCommand.Parameters.Add(new SQLiteParameter("@TolPlus", DbType.Decimal)); 
                    insertCommand.Parameters.Add(new SQLiteParameter("@OutOfTol", DbType.Decimal)); 
                    insertCommand.Parameters.Add(new SQLiteParameter("@Comment", DbType.String)); 

                    string[] files = Directory.GetFiles(Environment.CurrentDirectory, "TextFile*.*"); 

                    foreach (string file in files) 
                    { 
                        string[] lines = File.ReadAllLines(file); 
                        bool parse = false; 

                        foreach (string tmpLine in lines) 
                        { 
                            string line = tmpLine.Trim(); 
                            if (!parse && line.StartsWith("Feat. Type,")) 
                            { 
                                parse = true; 
                                continue; 
                            } 
                            if (!parse || string.IsNullOrEmpty(line)) 
                            { 
                                continue; 
                            } 


                            foreach (SQLiteParameter parameter in insertCommand.Parameters) 
                            { 
                                parameter.Value = null; 
                            } 

                            string[] values = line.Split(new[] {','}); 

                            for (int i = 0; i < values.Length - 1; i++) 
                            { 
                                SQLiteParameter param = insertCommand.Parameters[i]; 
                                if (param.DbType == DbType.Decimal) 
                                { 
                                    decimal value; 
                                    param.Value = decimal.TryParse(values[i], out value) ? value : 0; 
                                } 
                                else 
                                { 
                                    param.Value = values[i]; 
                                } 
                            } 

                            insertCommand.ExecuteNonQuery(); 
                        } 
                    } 
                } 
                con.Close(); 
            } 
        } 
    } 
} 

UPDATE:
I Tried this, as suggested below, but no go.. the column in the db just fills with random words from the text files.

    string RNumber = Regex.Match(filename, @"(R.*)_.*_\.txt").Groups[1].Value;
 insertCommand.Parameters.Add(new SQLiteParameter("@FileName", RNumber));
 insertCommand.CommandText = @" INSERT INTO Files (FileName) VALUES (@Filename)
  • 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-15T00:37:22+00:00Added an answer on May 15, 2026 at 12:37 am

    IF the pattern is always xxxxx_anythingelse.txt then you could parse out the xxx with:

    strFileName.Substring(0,strFileName.IndexOf("_"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thanks for a solution in C , now I would like to achieve this
Ok thanks for the 1st guy I found the code. Machine.Migrations: I mean, the
what is the code to disable an INPUT text box for HTML? Thanks
I was wondering what everyone thinks of this. Is the code easy to follow?
Thanks for reading this. I am dynamically generating some data which includes a select
I have written code below.but it will print this exception and i really don't
This piece of code below is causing my app to crash EDIT @interface termsAndConditions
This line of code is generating an error: Accessing unknown 'zPosition' component of a
I have created a WebView programmatically. This works perfect. The code is below. What
Thanks to a Q&A on stackoverflow. I just found out how to determine the

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.