dcsimg
Results 1 to 15 of 15

Thread: [RESOLVED] How to read all text file in 1 folder and put data to data gridview.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    75

    Resolved [RESOLVED] How to read all text file in 1 folder and put data to data gridview.

    hi Sir,

    I want to make a program log file organizers , i have many log files in a form of text file format. maybe thousands , each log files has the same format of data, i want to get only specific details from each log files and put it in data grid view so that it it easy to view.

    Program function and STATUS:
    1. auto read all text file in specified folder. -------as of now i click/choose manual text files.
    2. get specific data in text file.----------------DONE.
    3. put the data in text box.--------------------DONE.
    4. throw the data from text box to data grid view and save to sql server.-------DONE.


    as of now I can able to do all things already but by choosing or clicking the text file one by one... I don't like it, I want it to be automatic reading all text files inside the folder i selected.


    here is my code for manually selecting the log file..
    Code:
     //COPY TEXT FILE TO TEXTBOX
    
            void Copy()
            {
    //selecting log file manually one by one. 
                if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    path1 = openFileDialog1.FileName;
    
                    BinaryReader BR_Date = new BinaryReader(File.OpenRead(path1));
                    BR_Date.BaseStream.Position = 0x0;
                    foreach (char dates in BR_Date.ReadChars(20))
                    {
                        textBox1.Text += dates;
                        BR_Date.Dispose();
                    }
    
                    BinaryReader BR_Time = new BinaryReader(File.OpenRead(path1));
                    BR_Time.BaseStream.Position = 0x15;
                    foreach (char times in BR_Time.ReadChars(9))
                    {
                        textBox10.Text += times;
                        BR_Time.Dispose();
                    }
     // DISPLAY FUNCTION 
            void Display()
            {
                SqlDataAdapter sda = new SqlDataAdapter("Select * from logfileConverter", con);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                dataGridView1.Rows.Clear();
                foreach (DataRow item in dt.Rows)
                {
                    int n = dataGridView1.Rows.Add();
                    dataGridView1.Rows[n].Cells[0].Value = item[0].ToString();
                    dataGridView1.Rows[n].Cells[1].Value = item[1].ToString();
                    dataGridView1.Rows[n].Cells[2].Value = item[2].ToString();
                    dataGridView1.Rows[n].Cells[3].Value = item[3].ToString();
                    dataGridView1.Rows[n].Cells[4].Value = item[4].ToString();
                    dataGridView1.Rows[n].Cells[5].Value = item[5].ToString();
                    dataGridView1.Rows[n].Cells[6].Value = item[6].ToString();
                    dataGridView1.Rows[n].Cells[7].Value = item[7].ToString();
                }
            }
    
            //CLEAR
            void Clear()
            {
                textBox1.Clear();
                textBox10.Clear();
                textBox11.Clear();
                textBox12.Clear();
                textBox2.Clear();
                textBox3.Clear();
                textBox4.Clear();
                textBox5.Clear();           
            }
     private void btnRead_Click(object sender, EventArgs e)
            {
                Copy();
            }
    
            private void btnInsert_Click(object sender, EventArgs e)
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand(@"insert into logfileConverter
                    ([Serial_Number],[Date],[Time],[PWM Count V32DIV],[Delta of V32_Div is],[V32vDiv A2D VOLTAGE],[Measured Supply Voltage On VPP],[9 bits PWM Count VPP])values('" + textBox11.Text + "','" + textBox1.Text + "','" + textBox10.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox12.Text + "')", con);
                    cmd.ExecuteNonQuery();         
                    MessageBox.Show("Successfully save");
                    System.IO.File.Delete(path1);
                    Display();
                    Clear();
                    con.Close();
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }      
            }

    I tried this code below to auto copy 1 by 1 all text file in the selected path. but it is not working please help me , i am new to c# .
    Code:
      private void btnRead_Click(object sender, EventArgs e)
            {
    
                if (txtReadPath.Text != "")
                {
    
                    path1 = txtReadPath.Text;
                    var txtFiles = Directory.EnumerateFiles(path1,"@.txt");
                    foreach (string currentFile in txtFiles)
                    {
                        Copy();
                    }
                }
                else
                {
                    MessageBox.Show("select path");
                }
               
            }
    when click button readlogfile i need to do it one by one. ... I want it to auto select each text file. no need to click.
    Name:  1.jpg
Views: 295
Size:  37.3 KB

    i can copy specific details from text file to text box.. this works well.
    Name:  2.jpg
Views: 279
Size:  23.9 KB

    auto insert log file to data grid view. this works well.
    Name:  3.jpg
Views: 280
Size:  39.1 KB

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,600

    Re: How to read all text file in 1 folder and put data to data gridview.

    As the documentation states, the pattern should be "*.txt" to match every file with a ".txt" extension.

    As for being automatic, you just need to initiate the code from a handler for an event that the user doesn't have to cause, e.g. the Load event handler of the form. Everything starts with an event handler so any time you want to do anything, the first task is to determine when you want it to happen and what event corresponds to that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    75

    Re: How to read all text file in 1 folder and put data to data gridview.

    I want it to happen when i click the button "READ LOG FILE FROM" click event.
    as of now when i click it , it shows open file dialog to let me select the text file one by one.
    i don't want it. i want it to be automatic.

    i also put code in my button "INSERT NEW LOG FILE" to a text change event. so once get the data in text file and put in text box. automatic it throws to data grid view and clear all the text box , ready to receive the next data.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,600

    Re: How to read all text file in 1 folder and put data to data gridview.

    You already know how to do that because you're already doing it, loop through the result of Directory.EnumerateFiles. You just need to call that method properly, i.e. use the correct search pattern. Have you done what I have already told you to do? If not, why not?

    I should have to tell you this but please don't ever post something like this:
    but it is not working
    That's all but useless. You are there looking at it. You can see what actually happens. Why do you think it's a good idea to keep that information a secret? Do you go to the doctor and coyly refuse to describe your symptoms ands expect a diagnosis? I would think not.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    75

    Re: How to read all text file in 1 folder and put data to data gridview.

    hi Sir sorry i just copy that code to internet. how can i call that method ?
    should i use for loop ? Please show me how to code it. i'm really new to c#.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,600

    Re: How to read all text file in 1 folder and put data to data gridview.

    Maybe, instead of blindly copying code from the web, you should put some thought into what you're doing. Think about the steps that are actually involved and then write code to implement those steps. You are already getting the paths of the files and you are already looping through those paths. Inside that loop you call a Copy method but how is that method supposed to do anything useful if you don't pass it the path of the file you want to copy? You may be new to C# but you're not new to using logic so use it. Start by writing a method that takes the path of a file and copies that file. You can then call that method in a loop and pass it the paths of the files you want to copy. There's nothing specific to C# in that simple logic so being new to C# is irrelevant.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,600

    Re: How to read all text file in 1 folder and put data to data gridview.

    You need to apply some logic to these situations. This is how you should be thinking. You've got this method:
    Code:
            void Copy()
            {
    //selecting log file manually one by one. 
                if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    path1 = openFileDialog1.FileName;
    
                    BinaryReader BR_Date = new BinaryReader(File.OpenRead(path1));
                    BR_Date.BaseStream.Position = 0x0;
                    foreach (char dates in BR_Date.ReadChars(20))
                    {
                        textBox1.Text += dates;
                        BR_Date.Dispose();
                    }
    
                    BinaryReader BR_Time = new BinaryReader(File.OpenRead(path1));
                    BR_Time.BaseStream.Position = 0x15;
                    foreach (char times in BR_Time.ReadChars(9))
                    {
                        textBox10.Text += times;
                        BR_Time.Dispose();
                    }
    If you don't want to be using an OpenFileDialog then obviously you need to get rid of the code that uses one:
    Code:
            void Copy()
            {
    //selecting log file manually one by one. 
                    BinaryReader BR_Date = new BinaryReader(File.OpenRead(path1));
                    BR_Date.BaseStream.Position = 0x0;
                    foreach (char dates in BR_Date.ReadChars(20))
                    {
                        textBox1.Text += dates;
                        BR_Date.Dispose();
                    }
    
                    BinaryReader BR_Time = new BinaryReader(File.OpenRead(path1));
                    BR_Time.BaseStream.Position = 0x15;
                    foreach (char times in BR_Time.ReadChars(9))
                    {
                        textBox10.Text += times;
                        BR_Time.Dispose();
                    }
    Now you have a 'path1' variable you need a value for. It should be obvious that the solution is to pass in that value via a parameter:
    Code:
            void Copy(string path1)
            {
    //selecting log file manually one by one. 
                    BinaryReader BR_Date = new BinaryReader(File.OpenRead(path1));
                    BR_Date.BaseStream.Position = 0x0;
                    foreach (char dates in BR_Date.ReadChars(20))
                    {
                        textBox1.Text += dates;
                        BR_Date.Dispose();
                    }
    
                    BinaryReader BR_Time = new BinaryReader(File.OpenRead(path1));
                    BR_Time.BaseStream.Position = 0x15;
                    foreach (char times in BR_Time.ReadChars(9))
                    {
                        textBox10.Text += times;
                        BR_Time.Dispose();
                    }
    Now you simply change the way you call that method to pass in the path of the file. That is basic logic and nothing specifically to do with C#.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    75

    Re: How to read all text file in 1 folder and put data to data gridview.

    hi sir,

    i do as you said, but it is not calling the condition i put inside 2 curly brackets.
    even the messagebox.

    Code:
       private void btnRead_Click(object sender, EventArgs e)
            {
    
                if (txtReadPath.Text != "")
                {
    
                    path1 = txtReadPath.Text;
            
                    var txtFiles = Directory.EnumerateFiles(path1,"@.txt");
                    foreach (string currentFile in txtFiles)
                    {   
                        Copy(path1);
                        MessageBox.Show("success");
                    }
                }
                else
                {
                    MessageBox.Show("select path");
                }
    Code:
            void Copy(string path1)
            {
    
                  BinaryReader BR_Date = new BinaryReader(File.OpenRead(path1));
                    BR_Date.BaseStream.Position = 0x0;
                    foreach (char dates in BR_Date.ReadChars(20))
                    {
                        textBox1.Text += dates;
                        BR_Date.Dispose();
                    }
    
                    BinaryReader BR_Time = new BinaryReader(File.OpenRead(path1));
                    BR_Time.BaseStream.Position = 0x15;
                    foreach (char times in BR_Time.ReadChars(9))
                    {
                        textBox10.Text += times;
                        BR_Time.Dispose();
                    }
    
    
    
    
    
                    BinaryReader BR_Serial = new BinaryReader(File.OpenRead(path1));
                    BR_Serial.BaseStream.Position = 0x1E;
                    foreach (char Serial in BR_Serial.ReadChars(13))
                    {
                        textBox11.Text += Serial;
                        BR_Serial.Dispose();
                    }

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,600

    Re: How to read all text file in 1 folder and put data to data gridview.

    First of all, I have told you I-don't-know-how-many times already to use "*.txt" as the search pattern and yet you STILL have "@.txt" in your code. If you're just going to ignore what people tell you then what's the point of asking questions?

    Secondly, I think you need to go back and learn the basics of programming properly because you are NOT doing what I told you. You shouldn't need me to tell you to look at your own code and ask yourself whether it is actually doing what you want to do. Look at this:
    Code:
                    path1 = txtReadPath.Text;
            
                    var txtFiles = Directory.EnumerateFiles(path1,"@.txt");
                    foreach (string currentFile in txtFiles)
                    {   
                        Copy(path1);
                        MessageBox.Show("success");
                    }
    Is that seriously what you want to do? If 'path1' contain the path of the folder and 'currentFile' contains the path of the current file from that folder, do you really want to pass 'path1' to that Copy method? How does that make sense? This is why I keep on banging on about working out your algorithm first and, if necessary, writing it down. You can then read your code an compare each line to that algorithm to se whether the code actually implements the algorithm. If you have a step that talks about the current file and you're not actually using current file in the code that is supposed to implement that step then obviously your code is wrong.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,600

    Re: How to read all text file in 1 folder and put data to data gridview.

    May I also say that if you use sensible, logical, descriptive names for everything then mistakes like this can be fairly easily avoided. Names like 'path1' are just plain bad. If you had variables named 'folderPath' and 'filePath' then it would be completely obvious that you were using the wrong one. Don't be lazy when naming variables or anything else. Put some thought into what it exists for and use a name that clearly indicates that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    75

    Re: How to read all text file in 1 folder and put data to data gridview.

    i tried this also but not works..sorry sir i am not good.

    Code:
       private void btnRead_Click(object sender, EventArgs e)
            {
               string filePath = txtReadPath.Text;
            
                if (txtReadPath.Text != "")
                {
    
    
                    var txtFiles = Directory.EnumerateFiles(filePath, ".txt");
                    foreach (string currentFile in txtFiles)
                    {
                        Copy(currentFile);
                        MessageBox.Show("success");
                    }
                }
                else
                {
                    MessageBox.Show("select path");
                }

  12. #12
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,600

    Re: How to read all text file in 1 folder and put data to data gridview.

    This is ridiculous. How many times can I tell you the same thing? I've told you over and over to use "*.txt" and you have finally made a change from "@.txt" but you changed it to ".txt" instead of what I told you to change it to. If you can't even read what I'm writing then you're wasting my time. I understand that some things are hard but this is not one of them. I'm done here.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    75

    Re: How to read all text file in 1 folder and put data to data gridview.

    OMG ... how can i not understand it.. really sorry sir, i'm not really feeling well now. it is really ridiculous. sorry.. now i got it... thanks a millions..

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    75

    Re: [RESOLVED] How to read all text file in 1 folder and put data to data gridview.

    hi Sir,

    is it possible to read the text file 1 by 1 ?base on the time it was created ? because now it is reading automatic all the text file presents in the folder i selected. i want 1 after the other ? pls advised thanks.

  15. #15
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,600

    Re: [RESOLVED] How to read all text file in 1 folder and put data to data gridview.

    I really don't know what you're asking but, whatever it is, it's not "How to read all text file in 1 folder and put data to data gridview". You've already marked this thread Resolved and there was a reason for that: the issue you raised has been resolved. If you now have a new issue then you should start a new thread with a title that summarises that new issue and then provide all the information relevant to that issue and none that isn't. We can then address that issue specifically without erroneous clutter.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width