Solved

Read a file

Posted on 2006-11-15
5
171 Views
Last Modified: 2010-04-30
Hi guys,

I have a text file that has lines like these:

32/12/3312#__/__/____#__:__ __#__:__ __#1
31/23/123_#__/__/____#__:__ __#__:__ __#5
_3/12/312_#__/__/____#__:__ __#__:__ __#9

How can I read this file line by line?

Thanks.
0
Comment
Question by:RealSnaD
5 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 17951796
Something like...

    Dim ff As Long
    Dim fileName As String
    Dim inputLine As String
   
    ff = FreeFile
    fileName = "c:\someFile.txt"
    Open fileName For Input As #ff
    While Not EOF(ff)
        Line Input #ff, inputLine
       
        ' do something with "inputLine"...
        Debug.Print inputLine
    Wend
    Close #ff
0
 
LVL 1

Expert Comment

by:JReam
ID: 17951931

Dim fso As New FileSystemObject
Dim MyFile as File, inputLine$

Set MyFile = fso.OpenTextFile("c:\testfile.txt", ForReading)

While not MyFile.AtEndOfStream

   inputLine = MyFile.ReadLine

   Debug.Print inputLine

Wend

MyFile.Close


0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17952024
For JReam's code to work, you need a reference to the Scripting Runtime.  Click on Project --> References and check the "Microsoft Scripting Runtime" entry.

Then you need to change the declaration for the "MyFile" variable as it should be a "TextStream" and not a "File":

    Dim fso As New FileSystemObject
    Dim MyFile As TextStream
    Dim inputLine As String
   
    Set MyFile = fso.OpenTextFile(("c:\testfile.txt", ForReading)
    While Not MyFile.AtEndOfStream
        inputLine = MyFile.ReadLine
        Debug.Print inputLine
    Wend
    MyFile.Close

~IM
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 17952736
Hi RealSnaD,

Using JReam's approach, but no reference required:

    Dim fso As Object
    Dim MyFile As Object
    Dim inputLine As String
   
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set MyFile = fso.OpenTextFile("c:\testfile.txt", ForReading)
    While Not MyFile.AtEndOfStream
        inputLine = MyFile.ReadLine
        Debug.Print inputLine
    Wend
    MyFile.Close

    Set MyFile = Nothing
    Set fso = Nothing

Regards,

Patrick
0
 
LVL 7

Author Comment

by:RealSnaD
ID: 17957888
Thanks you all. Idle_Mind you were first with a working solution.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now