Thursday, April 14, 2011

Read multiple text files in a folder

I have many text files in a folder. What I can do now is to read through one text at a time and insert it into the database. My little app reads a text file when I debug it. So, I need to run it several times to read all those text files and import them into the database.

My question is how to read multiple text files inside a folder at a time. Here's my code which works fine but it reads only one text files at a time.

Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click

        Dim filelocation As String
        filelocation = "F:\txtfiles\ch25.txt"
        Dim chid As Integer
        chid = 25



        'read from file'
        Dim MyStream As New StreamReader(Path.Combine(Application.StartupPath, filelocation))
        Dim vArray() As String = MyStream.ReadToEnd.Split(CChar("$"))
        MyStream.Close()



        Dim count As Integer

        'insert text to table'
        For d As Integer = 0 To vArray.Length - 1 Step 1

            If d = vArray.Length - 1 Then
                Exit For
            End If

            InsertKh(chid, d + 1, vArray(d))
            count = d + 1
        Next


       MsgBox ("Done Inserting")

End Sub

Obviously, I need a way to loop through a folder and check if there's text file. But I cant get it right. Can anyone show me some code or links ? I'm using VB.NET, .NET 3.5

Thanks so much.

From stackoverflow
  • I would look into using the ThreadPool.QueueUserWorkItem. Basically you'll want to read all the files in the Folder and enqueue each file for processing. You'll have to construct a method that can process each file individually as its own sub routine.

    Angkor Wat : Alright, I'm reading the link you posted. Thanks.
    bendewey : I may have taken your question to the next step. My recommendation is in regards to multi-threading the reading of your files to improve performance.
    Nathan Koop : @bendewey, I agree, I think his question centered around functionality, not performance.
  • Look into Directory.GetFiles.

    You can call it with a specified search pattern like "*.txt" to find a specific kind of file. Something like this:

        Dim fileEntries As String() = Directory.GetFiles(targetDirectory,"*.txt")
        ' Process the list of .txt files found in the directory. '
        Dim fileName As String
        For Each fileName In fileEntries
            ProcessFile(fileName)
    
  • Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
    
    Dim di As New DirectoryInfo("F:\txtfiles")
    Dim s As String
    
    ForEach s In di.GetFiles("*.txt")
        Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
            Dim filelocation As String
            filelocation = s ''"F:\txtfiles\ch25.txt"
            Dim chid As Integer
            chid = 25
    
    
    
            'read from file'
            Dim MyStream As New StreamReader(Path.Combine(Application.StartupPath, filelocation))
            Dim vArray() As String = MyStream.ReadToEnd.Split(CChar("$"))
            MyStream.Close()
    
    
    
            Dim count As Integer
    
            'insert text to table'
            For d As Integer = 0 To vArray.Length - 1 Step 1
    
                If d = vArray.Length - 1 Then
                    Exit For
                End If
    
                InsertKh(chid, d + 1, vArray(d))
                count = d + 1
            Next
    
    
          '' MsgBox ("Done Inserting")
    
    
        End Sub
    
  • Use the Directory.GetFiles method to find all text files in a folder:

    Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
    
       Dim files As String() = System.IO.Directory("F:\txtfiles\", "*.txt")
    
       Dim count As Integer = 0
    
       ForEach filelocation As String in files
    
          Dim chid As Integer = 25
    
          'read from file'
          Dim MyStream As New StreamReader(filelocation)
          Dim vArray() As String = MyStream.ReadToEnd.Split("$"C)
          MyStream.Close()
    
          'insert text to table'
          For d As Integer = 0 To vArray.Length - 2
             InsertKh(chid, d + 1, vArray(d))
             count = count + 1
          Next
    
       Next
    
       MsgBox ("Done Inserting")
    
    End Sub
    

    Notes:
    Using Path.Combine when the second parameter is a complete path just returns the second parameter, so it's useless in this case.
    Instead of converting a string to a char, you can write a char literal like this: "$"C.
    Instead of breaking out of the loop when you are at the last item, just make the loop end one item earlier.
    As you have nested loops you have to increase the counter in the loop instead of assigning the loop variable too it, otherwise you will only get the number of items in the last file. (If you are actually going to use the counter for anything.)

0 comments:

Post a Comment