dcsimg
Results 1 to 4 of 4

Thread: how to read text files inside the folder 1 by 1 and delete it after reading finish

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    75

    how to read text files inside the folder 1 by 1 and delete it after reading finish

    hi sir,

    I want to improve my program,

    1. read each text file inside the folder 1 by 1 . after i read the data inside the text file and put it in text box and auto insert in database . i want to delete the text file automatic then find the next text file and do also the same until all text file in the folder are finish.


    as of now my code read all the text file in the folder i want. pls help me change it to get text file 1 by 1 .
    1 after the others.

    thank you in advanced.

    Code:
       private void btnRead_Click(object sender, EventArgs e)
              {
    
                  if (txtReadPath.Text != "")
                  {
    
    
                     folderPath = txtReadPath.Text ;
                      var txtFiles = Directory.GetFiles(folderPath, "*.txt");
                      foreach (string currentFile in txtFiles)
                      {
                        
                          Copy(currentFile);
                       //  System.IO.File.Delete(folderPath);
         
                      }
                    
                  }
    
                  else
                  {
                      MessageBox.Show("select path");
                  }
              }

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

    Re: how to read text files inside the folder 1 by 1 and delete it after reading finis

    It already is doing it one by one. That's the whole point of a loop. You are calling Copy for each text file in the folder. First you call Copy for the first file in the folder, then you call it for the second file, then the third, etc.

    Are you actually saying that you want to read one file each time the user clicks the Button? If so then you need to read the list of files and store them somewhere, then read take the next file from the list on each click.
    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 text files inside the folder 1 by 1 and delete it after reading finis

    hi Sir,

    I was also expecting that it will do one by one the moment i used foreach loop. but pls see the result of my program.


    below is the folder that contains text files it has 3 records. the moment i click the button i want my program to get the data i want in each log file 1 after another and put it first in the text box and then put it to data grid view. again file by file.
    Name:  5.jpg
Views: 26
Size:  17.1 KB

    unfortunately my program do like this. when i click button it get all the data of each text file all together and put in my text box. it cannot put in my data grid view because the character is more than as i assign.
    please advise me how can i get data file by file and not all together Sir.

    Name:  4.PNG
Views: 26
Size:  18.0 KB


    would it be because on my code highlighted below ?not sure.
    Code:
      void Copy(string filePath)
            {
                BinaryReader BR_Date = new BinaryReader(File.OpenRead(filePath));
                    BR_Date.BaseStream.Position = 0x0;
                    foreach (char dates in BR_Date.ReadChars(20))
                    {
                        textBox1.Text = textBox1.Text+ dates;
                        BR_Date.Dispose();
                    }
    
                    BinaryReader BR_Time = new BinaryReader(File.OpenRead(filePath));
                    BR_Time.BaseStream.Position = 0x15;
                    foreach (char times in BR_Time.ReadChars(9))
                    {
                        textBox10.Text += times;
                        BR_Time.Dispose();
                    }

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    75

    Re: how to read text files inside the folder 1 by 1 and delete it after reading finis

    THE purpose of this program Sir is to summary or put all the data of each text file in one data grid view or excel file for easy viewing.
    no need to open each text file.
    sample output below.

    Name:  6.jpg
Views: 25
Size:  12.8 KB

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