Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting Lines in a text file 1

Status
Not open for further replies.

PcLinuxGuru

Technical User
Nov 17, 2001
178
US
I have a text file that I need to delete every other line.

So I need to make this:
12:51:53 PM Thu Mar 10, 2005 00001 CHARGE 32.86 0.00 FALSE FALSE
Emp 200 Check 2097154 Tender 10
12:13:00 PM Thu Mar 10, 2005 00001 CHARGE 26.01 5.00 FALSE FALSE
Emp 999 Check 1048577 Tender 10
07:19:31 PM Mon Mar 14, 2005 01004 CHARGE 9.90 2.00 FALSE FALSE
Emp 201 Check 2097167 Tender 10

Look like this:
12:51:53 PM Thu Mar 10, 2005 00001 CHARGE 32.86 0.00 FALSE FALSE
12:13:00 PM Thu Mar 10, 2005 00001 CHARGE 26.01 5.00 FALSE FALSE
07:19:31 PM Mon Mar 14, 2005 01004 CHARGE 9.90 2.00 FALSE FALSE

Any suggestions or point me to where I should be looking?

 
Open VB
click Project -> Reference
Scroll to "Microsoft Scripting Runtime" and check it

put a button on the form

copy the following code.

Code:
Option Explicit

Private Sub Command1_Click()
    
    Dim FSO As Scripting.FileSystemObject
    Dim arData() As String
    Dim strData As String
    Dim arResult() As String
    Dim i As Long
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    ' Make sure the file exists
    If Not FSO.FileExists("C:\Temp.txt") Then
        Exit Sub
    End If
    
    ' open the file and read all the data
    strData = FSO.GetFile("C:\Temp.txt").OpenAsTextStream(ForReading).ReadAll
    
    ' Split the data in to an array
    arData = Split(strData, vbCrLf)
    
    ' create the result array
    ReDim arResult(UBound(arData) / 2)
    
    ' loop through the original data, stepping by 2 to skip rows
    For i = LBound(arData) To UBound(arData) Step 2
        arResult(i / 2) = arData(i)
    Next
    
    ' recreate a big string
    strData = Join(arResult, vbCrLf)
    
    ' save the result to a file.
    Call FSO.CreateTextFile("C:\Output.txt", True, False).Write(strData)
    Set FSO = Nothing
    
End Sub



-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Dang I was close :) I tried using fso but for the life of me I could not figure out a way to skip the lines :)

Thanks!!!!



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top