Monday, 25 February 2013

Reading all text files in directory

Presently this code opens one .txt file at time, reads the data and then populates a user interface:
Code:

        OpenFD.InitialDirectory = "C:\Program Files (x86)\EPC\Ace"
        OpenFD.Title = "Open a Text File"
        OpenFD.Filter = "Text Files|*.txt|All Files|*.*"
        OpenFD.FileName = ""
        If OpenFD.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim Lines = _
        ( _
            From line In IO.File.ReadAllLines(OpenFD.FileName) _
            Where line.Length > 0 AndAlso _
            Not line.StartsWith("5") AndAlso _
            Not line.StartsWith("9") _
        ).ToList
            For Each line In Lines
                txtAccountNumber.Text = line.Substring(21, 4)
                txtAmountPaid.Text = line.Substring(62, 7)
            Next
      End If

Then the data is saved to an Access DB.

Since many txt files are in the directory, it is desirable to read them without using an OpenFileDialog. Eliminate the .showdialog and add a
For each
'reading each text file, extracting the data as above and then saving the data before moving to the next text file. the if then end if would be removed.

However, my level of expertise is limited on how to grab each text file. Any help would be great. Thanks.

Reading all text files in directory

No comments:

Post a Comment