Solved

How can I skip specific lines?

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

770 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