Abbas1999
Abbas1999

Reputation: 395

Reading File of Certain Sequence

In VB.NET; I am trying to read text file (say 5th text file in folder); My code below read all files and write down the values of LAST file only:

Imports System
Imports System.IO

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim MekdamFolder As String
    MekdamFolder = Application.ExecutablePath.Remove(Application.ExecutablePath.LastIndexOf("\") + 1)
    Dim MekdamFile As String
    Dim MekdamFiles As String() = Directory.GetFiles(MekdamFolder, "*.txt")
    For Each MekdamFile In MekdamFiles
    Next
    If System.IO.File.Exists(MekdamFile) Then
        IO.File.OpenText(MekdamFile)
        TextBox1.Text = System.IO.Path.GetFileNameWithoutExtension(MekdamFile)
    Else
        MessageBox.Show("MekdamFile Missing")
    End If
    Dim lines() As String = IO.File.ReadAllLines(MekdamFile)
    Dim lineArray As New ArrayList(1000)
    For zzzz As Integer = 0 To lines.GetUpperBound(0)
        lineArray.Add(lines(zzzz))
        RichTextBox1.AppendText(lines(zzzz) & vbCrLf)
    Next
End Sub

End Class

Any help appreciated.

Upvotes: 0

Views: 111

Answers (1)

first, last, fifth - the filenamess are returned to you in an arbitrary order. todays, first may be tomorrows tenth.

' this loop does nothing, except leave Mekdam set to the last file
For Each MekdamFile In MekdamFiles
   ' add code here to Exit For when MekdamFile 
   ' equals something meaningful
Next

' this can currently only edit the last file found due to the above loop.
If System.IO.File.Exists(MekdamFile) Then
    IO.File.OpenText(MekdamFile)
    TextBox1.Text = System.IO.Path.GetFileNameWithoutExtension(MekdamFile)
Else
....

You'd be better off looking for a specific file or getting it from the user.

Upvotes: 1

Related Questions