Solved

How can I skip specific lines?

Posted on 2000-03-27
5
198 Views
Last Modified: 2010-05-02
I have written code to go through a text file and extract the data I need. It is a delimited file so I use the comma to divide the values. The problem that I am experiencing is that the lines which includes data I need begin with a "~" but there are somelines beginning with a "#". These lines I need to be able to skip. The real obstacle is that the TextStreamObject method to skip lines (eg. v.skiplines(1)) actually skips two lines, the current line and the new line character of the next line. The next line almost always includes data I need. I have used the TextStreamObject with the entire code so I guess I need to stick with it. How do I skip just the line I need to skip and leave everything else intact?
0
Comment
Question by:danetteh
5 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2661478
could it be possible to use the file system object with the readline method and then based on the first character of the line determine whether to extract that particular line?
0
 

Author Comment

by:danetteh
ID: 2661613
Thanks for the response. To determine whether or not I need to read the line does depend on the first character, but if the first character is a "#", I still need to skip it. That's where the problem lies. The .skipline(x) method in VB skips two lines instead of one. I'm wondering if there's a way to just skip the one, or an example of code to read only the lines I need and it would automatically not even read the rest of a line not beginning with a "~".
D
0
 

Author Comment

by:danetteh
ID: 2661615
Adjusted points from 50 to 75
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2661701
Well, here's a function I use to read a file into an array... Lines begining with "#" are skipped.

'-------------------------------------------------------------------------------
    Function GetFileArray(FileName As String) As Variant
        Dim Lines() As String
        Dim fNum As Long
        fNum = FreeFile
        ReDim Preserve Lines(0) As String
        Open FileName For Input Access Read As #fNum
        Do While Not EOF(fNum)
            Line Input #fNum, Lines(UBound(Lines))
            If Not Left$(Trim$(Lines(UBound(Lines))), 1) = "#" Then
                ReDim Preserve Lines(UBound(Lines) + 1) As String
            End If
        Loop
        On Error Resume Next
        ReDim Preserve Lines(UBound(Lines) - 1) As String
        If Err = 0 Then GetFileArray = Lines
    End Function
'-------------------------------------------------------------------------------



To use the function, you do this:

    Dim FileLines As Variant
    Dim iVal As Long
   
    FileLines = GetFileArray("C:\windows\desktop\testfile.txt")
    If IsEmpty(FileLines) = False Then
        For iVal = 0 To UBound(FileLines)
            Debug.Print "GOT:" + FileLines(iVal)
        Next
    End If



Cheers!®©



0
 
LVL 32

Accepted Solution

by:
bhess1 earned 75 total points
ID: 2661784
I'm just wondering where the syntax v.skipline(1) came from?  All of the references that I have to the method refer to .skipline  -- no trailing parameter.  ADO, VB6, etc.  all agree.  Try without the terminal (1) and see.

Also, what code are you using to read in the text?  if you are using the .readline method, then just skip your processing whenever the first character is not a "~", e.g.

Dim sLine as String

Do While Not tStream.atEndOfStream
  sLine = tStream.ReadLine
  If Left(sLine,1) = "~" Then
    '  Process Line Here
  End If
Loop
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

Suggested Solutions

Title # Comments Views Activity
MS Access Search and Replace Using VBA 6 57
Exit a vb6 apps when a calling it apps closes 15 41
Recommendation vb6 to vb.net or others 14 105
Help me. 3 43
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

930 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

11 Experts available now in Live!

Get 1:1 Help Now