Solved

How can I skip specific lines?

Posted on 2000-03-27
5
197 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

744 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