Solved

Deleting header lines in text file

Posted on 2003-11-26
3
386 Views
Last Modified: 2012-05-04
I am writing data to an appending text file.  However, each batch file that is written has a header line(these are all the same).  The first header line is fine, but I don't not want any more subsequent headers after that.  Here is the code I have now, but for some reason it is not deleting any of my header lines...  Any suggestions or other possible solutions?

Header= <COLUMNS>|OFFICE_ID|TERMINAL_ID|OFFICE_NAME|PHONE....>

Code:
Open "c:\officeOutput.txt" For Append As #2
       
 'delete extra header lines
 For i = 0 To UBound(lines)
    str = lines(i)
    If InStr(str, "COLUMNS") > 0 Then
        lines(i) = vbNullChar
    End If
Next

 'uses filter to delete lines
lines = Filter(lines, vbNullChar, False)

 'write data
For j = 1 To UBound(lines)
      Print #2, getField(j,1) + getField(j,2)+getField(j,3)....
Next

Close#2
0
Comment
Question by:jennifere
3 Comments
 
LVL 14

Assisted Solution

by:aelatik
aelatik earned 125 total points
ID: 9825135
I made a function that does the job,

Function DeleteLineContaining(Filename As String, LineID As String)
    Dim BUFFER As New Collection
    Dim MYLINE As Variant
    Open Filename For Input As #1
        While Not EOF(1)
            Line Input #1, MYLINE
            BUFFER.Add MYLINE
        Wend
    Close #1
    Open Filename For Output As #1
        For Each MYLINE In BUFFER
            If InStr(1, MYLINE, LineID, vbTextCompare) = 0 Then
                Print #1, MYLINE
            End If
        Next
    Close #1
End Function

Private Sub Form_Load()
    Call DeleteLineContaining("c:\officeOutput.txt", "COLUMNS")
End Sub
0
 
LVL 10

Accepted Solution

by:
ADSaunders earned 125 total points
ID: 9825153
Hi jennifere,
I'm not sure what you are trying to achieve here, nor where the data is coming from to be appended to the file.
From your code it seems that some procedure is creating an array of lines which you then wish to append to the output file. If I am reading correctly, you don't wish to append any header lines. (-where are they coming from ??)
In any case, I would simlify to the following:

Open "c:\officeOutput.txt" For Append As #2
For j = 1 To UBound(lines)
    If InStr(lines(j), "COLUMNS") = 0 Then
      Print #2, getField(j,1) + getField(j,2)+getField(j,3)....
    end if
Next
Close#2

Regards .. Alan
0
 

Author Comment

by:jennifere
ID: 9825311
Okay, never mind.  I figured out the problem on my own.  I was writing my data to a collection and then trying to delete lines.  Anyhow, with a little bit of code rearrangement, it is working now.  I will go ahead and split the points because you both responded so quickly.  I appreciate it!  =)
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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…

863 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

22 Experts available now in Live!

Get 1:1 Help Now