Question Extracting specific data from text file based on date

zhb12810

New member
Joined
Oct 24, 2012
Messages
3
Programming Experience
Beginner
Hi,

I have the following basic code that extracts all the data from a text file and deposits it into a listbox (zMailbox is declared at the top of the class)

VB.NET:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


        Dim oReadMenuFunction As System.IO.StreamReader
        oReadMenuFunction = IO.File.OpenText(zMailbox)


        Do While oReadMenuFunction.Peek <> -1
            Dim LineIn As String = oReadMenuFunction.ReadLine()
            ListBox1.Items.Add(LineIn)
        Loop


        
    End Sub

The information contained in the text box looks like this...

Unit Name =
Unit Code =
Operation =
Requirements =
Last Audit Date =
Last Auditor =
Date Planned =

Within the folder that text file is located there could be up to 20 different text files but with differing data. What I'm struggling with is adapting the code to look through all the text files and if the line "Date Planned = " matches the date selected from a date picker then to copy and append the information after the "=" on the lines Unit Code, Unit Name, Operation and apply to the listbox

Any advice would be gratefully received

Thanks in advance
 
Hi and welcome to the Forum,

Well the first thing to do is iterate through the different files that may reside in the Directory in question. To do that have a look here:-

Directory.GetFiles Method (String) (System.IO)

The next thing to do, and assuming that each file is of the same structure that you have demonstrated in your post, is to read all the lines of each file into an array. To do this have a look here:-

File.ReadAllLines Method (String) (System.IO)

Once done, you will then need to use a For Each Loop to iterate through each element of the array and see if any elements of the array are equal to a combination of the string " Date Planned = " and the value of your DateTime picker, in your required string format, are equal to that string which is in your file. If so, then you need to take each element of the array in turn to construct and add what you want to the ListBox. If this is not the case then you would just ignore the file that has just been read and move onto the next one.

Hope that helps.

Cheers,

Ian
 
Back
Top