elmonko
elmonko

Reputation: 685

Search line in text file and return value from a set starting point vb.net

I'm currently using the following to read the contents of all text files in a directory into an array

 Dim allLines() As String = File.ReadAllLines(txtfi.FullName)

Within the text files are only 6 lines that all follow the same format and will read something like

forecolour=black

I'm trying to then search for the word "forecolour" and retrieve the information after the "=" sign (black) so i can then populate the below code

AllDetail(numfiles).uPath = ' this needs to be the above result

I've only posted parts of the code but if it helps i can post the rest. I just need a little guidance if possible

Thanks

This is the full code

Dim numfiles As Integer

    ReDim AllDetail(0 To 0)
    numfiles = 0
    lb1.Items.Clear()

    Dim lynxin As New IO.DirectoryInfo(zMailbox)

    lb1.Items.Clear()

    For Each txtfi In lynxin.GetFiles("*.txt")

        Dim allLines() As String = File.ReadAllLines(txtfi.FullName)


        ReDim Preserve AllDetail(0 To numfiles)

        AllDetail(numfiles).uPath = 'Needs to be populated
        AllDetail(numfiles).uName = 'Needs to be populated
        AllDetail(numfiles).uCode = 'Needs to be populated
        AllDetail(numfiles).uOps =  'Needs to be populated

        lb1.Items.Add(IO.Path.GetFileNameWithoutExtension(txtfi.Name))
        numfiles = numfiles + 1



    Next


End Sub

AllDetail(numfiles).uPath = Would be the actual file path
    AllDetail(numfiles).uName = Would be the detail after “unitname=”
    AllDetail(numfiles).uCode = Would be the detail after “unitcode=”
    AllDetail(numfiles).uOps =  Would be the detail after “operation=”

Within the text files that are being read there will be the following lines

Unitname=
Unitcode=
Operation=
Requirements=
Dateplanned=

For the purpose of this array I just need the unitname, unitcode & operation. Going forward I will need the dateplanned as when this is working I want to try and work out how to only display the information if the dateplanned matches the date from a datepicker. Hope that helps and any guidance or tips are gratefully received

Upvotes: 0

Views: 338

Answers (1)

Steve
Steve

Reputation: 216293

If your file is not very big you could simply

Dim allLines() As String = File.ReadAllLines(txtfi.FullName)
For each line in allLines
   Dim parts = line.Split("="c)
   if parts.Length = 2 andalso parts(0) = "unitname" Then
       AllDetails(numFiles).uName = parts(1)
       Exit For
   End If

Next

If you are absolutely sure of the format of your input file, you could also use Linq to remove the explict for each

Dim line = allLines.Where(Function(x) (x.StartsWith("unitname"))).SingleOrDefault()
if line IsNot Nothing then
    AllDetails(numFiles).uName = line.Split("="c)(1)
End If

EDIT

Looking at the last details added to your question I think you could rewrite your code in this way, but still a critical piece of info is missing.

What kind of object is supposed to be stored in the array AllDetails?

I suppose you have a class named FileDetail as this

Public class FileDetail
    Public Dim uName As String
    Public Dim uCode As String
    Public Dim uCode As String
End Class

....

numfiles = 0
lb1.Items.Clear()

Dim lynxin As New IO.DirectoryInfo(zMailbox)

' Get the FileInfo array here and dimension the array for the size required
Dim allfiles = lynxin.GetFiles("*.txt")

' The array should contains elements of a class that have the appropriate properties
Dim AllDetails(allfiles.Count) as FileDetail

lb1.Items.Clear()

For Each txtfi In allfiles)
    Dim allLines() As String = File.ReadAllLines(txtfi.FullName)

    AllDetails(numFiles) = new FileDetail()
    AllDetails(numFiles).uPath = txtfi.FullName

    Dim line = allLines.Where(Function(x) (x.StartsWith("unitname="))).SingleOrDefault()
    if line IsNot Nothing then
        AllDetails(numFiles).uName = line.Split("="c)(1)
    End If

    line = allLines.Where(Function(x) (x.StartsWith("unitcode="))).SingleOrDefault()
    if line IsNot Nothing then
        AllDetails(numFiles).uName = line.Split("="c)(1)
    End If

    line = allLines.Where(Function(x) (x.StartsWith("operation="))).SingleOrDefault()
    if line IsNot Nothing then
        AllDetails(numFiles).uOps = line.Split("="c)(1)
    End If

    lb1.Items.Add(IO.Path.GetFileNameWithoutExtension(txtfi.Name))
    numfiles = numfiles + 1
Next

Keep in mind that this code could be really simplified if you start using a List(Of FileDetails)

Upvotes: 1

Related Questions