Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How can I skip specific lines?

Posted on 2000-03-27
5
Medium Priority
?
207 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:
Brendt Hess earned 225 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…
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…
Suggested Courses

876 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