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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file scripting - readfile 1

Status
Not open for further replies.

whoffman

Programmer
Jun 12, 2002
28
0
0
US
Hopefully someone can enlighten me. I am trying to read a large (33MB) text file created in a Unix environment. It has no line feeds, just carriage returns (or the other way around). I am trying to read into a user defined type. That doesn't work and the error code is cryptic, at best. I tried reading with the scripting function, and it works well ( .readfile), but I can do nothing with it except debug.print. When I try to set it to a variable, I get an error "Object does not support method or property". What CAN you do with it? -OR- Is there a simple way to read in variable length records into a "type"?

TIA
 
Whenever I read from a file, I use the FileSystemObject.

1. Read all the data in to a string field
2. Parse the data in to an array uing the split function
3. Parse each record using the split function again.
4. Assign each element of the user defined data type.

Code:
Private Sub LoadData(ByVal FileName As String)

    Dim FSO As Scripting.FileSystemObject
    Dim cAllData As String
    Dim arTemp() As String
    Dim arData() As String
    Dim i As Long
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    If FSO.FileExists(FileName) Then
        cAllData = FSO.GetFile(FileName).OpenAsTextStream(ForReading).ReadAll
        End If
    End If
    Set FSO = Nothing
    
    If cAllData = "" Then
        Exit Sub
    End If
    
    arTemp = Split(cAllData, [green][b]vbCrLf[/b][/green])
    ReDim MyUDT(UBound(arTemp))
    
    For i = LBound(arTemp) To UBound(arTemp)
        If arTemp(i) <> "" Then
            arData = Split(arTemp(i), "[green][b],[/b][/green]")
            With MyUDT(i)
                .A = arData(0)
                .b = arData(1)
                .c = arData(2)
        End If
    Next i

End Sub

Of course, you can replace vbCrLf with vbCr or vbLf depending on your needs.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I know of a VB program that you can use to convert your unix file to a windows type text/data file with a specified line width that has normal CRLF's. Then I believe it would be much easier to load the converted text file. I can tell you how to download an evaluation copy if desired.
 
Thanks to all! Thanks George! After tweaking it a bit (I don't have the same (or any) Global.bas file) it worked fine. Much slower than I thought it would be to read 34 MB but its tolerable. Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top