Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help - pick files - read each one - load into grid - sort by headder.

Status
Not open for further replies.

coorsman

Technical User
Joined
Nov 25, 2008
Messages
1,113
Location
US

I have a list of files

c:\user\data\TXT00001
c:\user\data\TXT00002
c:\user\data\TXT00003
etc:

Each file contains:
BEGIN
DATE 11-08-2010
TIME 1345
USER KEVIN
TERMINAL TERMINAL1
...
...
...
END

There are about 32 headder descriptions, each file may not contain all headders and fields so when parsed they need to be placed into the correct array according to headder in each file.

I would like to use something like OpenFileDialog, select multiple files, open files, read in data lines, parse lines, and view/print in datagrid type of panel. I need to be able to sort by multiple headders and print.

This is my first venture using VB and I am and old basic and QB programmer.

Thanks in advance.
Coorsman

 

Here's some code to get you started.

To loop through, open and read the selected files:
Code:
Dim sr As System.IO.StreamReader

Dim ThisLine as string = ""

For Each FileName As String In OpenFileDialog1.FileNames
    sr = New System.IO.StreamReader(FileName)
    
    'read the file line by line
    Do While sr.Peek <> -1
        'do stuff with lines read
        ThisLine = sr.ReadLine
    Loop
Next

To display the data in a DataGridView, you could create a class for each header type, like so:
Code:
Public Class Header1
    Dim sField1 As String
    Dim sField2 As String

    Public Property Field1() As String
        Get
            Return sField1
        End Get
        Set(ByVal value As String)
            sField1 = value
        End Set
    End Property

    Public Property Field2() As String
        Get
            Return sField2
        End Get
        Set(ByVal value As String)
            sField2 = value
        End Set
    End Property
End Class

Now, as you parse the files create new instances of the appropriate class and populate its properties. Assign those class instances to an arry of that class type;
Code:
Dim Header1Array() As Header1

For Each FileName As String In OpenFileDialog1.FileNames
    sr = New System.IO.StreamReader(FileName)
    
    If Header1Array Is Nothing Then
        ReDim Header1Array(0)
    Else
        ReDim Preserve Header1Array(Header1Array.Length)
    EndIf

     
    'read the file line by line
    Do While sr.Peek <> -1
        'do stuff with lines read
        ThisLine = sr.ReadLine

        Header1Array(Header1Array.Length - 1) = New Header1
        Header1Array(Header1Array.Length - 1).Field1 = ThisLine.Substring(0, 3)
        Header1Array(Header1Array.Length - 1).Field2 = ThisLine.Substring(3, 6)

        'note: these are just examples, what you do will be different
    Loop
Next

Finally, you can assign the array of objects to a DataGridView, which will display them.
Code:
DataGridView1.DataSource = Header1Array

DataGridView1.Columns(0).HeaderText = "Column 1"
DataGridView1.Columns(0).HeaderText = "Column 2"

I hope this gets you going toward a solution. There are other ways to display the data in a grid (e.g., in-memory datatable), but you will still need to use the System.IO.StreamReader to read the files.



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
SWEETTTT, I'll give this a try later on tonite.

coorsman!
I owe you one.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top