Solved

How can I skip specific lines?

Posted on 2000-03-27
5
203 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Determine Range to Select 5 64
VB6 - Convert HH:MM into Decimal 8 76
Error with a code discussed on this page 5 51
MS SQL Update query with connected table data 3 81
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 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…

751 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