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!

Delete First Line of Text File 1

Status
Not open for further replies.

PcLinuxGuru

Technical User
Nov 17, 2001
178
US
I have a text file and in order for me to be able to use it I need to delete the first line. I have some code that will separate data and take the data I need and put it into a text file but when it does it creates a crlf at the top of the file and I am unable to figure out how to get rid of it without doing it manually.

I have tried a few things like this worked but it would also delete every other line rather just the first one:

Code:
Set FSO = CreateObject("Scripting.FileSystemObject")
    
    ' Make sure the file exists
 If Not FSO.FileExists("C:\pcs\temp\output.txt") Then
   lblStatus.Caption = ("Output.txt file is missing")
   Exit Sub
  End If
   
    ' open the file and read all the data
   strData2 = FSO.GetFile "C:\pcs\temp\Output.txt").OpenAsTextStream(ForReading).ReadAll
    strData2 = FSO.GetFile("C:\output.txt").OpenAsTextStream(ForReading).ReadAll
    ' Split the data in to an array
    arData2 = Split(strData2, vbCrLf)
    
    ' create the result array
    ReDim arResult2(UBound(arData2) / 2)
    
    For r = LBound(arData2) To UBound(arData2)
        arResult2(r / 2) = arData2(r)
    Next
    
    ' recreate a big string
    strData2 = Join(arResult2, vbCrLf)
    
    ' save the result to a file.
    Call FSO.CreateTextFile("C:\pcs\temp\Output2.txt", True, False).Write(strData2)
    Set FSO = Nothing

Any ideas?

 
SInce I do not see an edit button I am letting you know I do have the correct path.
it is c:\

 
Don't make such a big array...

' create the result array
ReDim arResult2(UBound(arData2) / 2 - 1)

Then, don't start at the lower bound. Start at the lower bound + 1

For r = (LBound(arData2) + 1) To UBound(arData2)
arResult2(r / 2) = arData2(r)
Next



-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I may have mis-understood your question. Jumped the gun. Here is a function that opens a text file, removes the first line, and then re-saves the text file.

Code:
Private Sub Command1_Click()
    
    Dim FSO As Scripting.FileSystemObject
    Dim cData As String
    Dim arData() As String
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    
    ' read all the data
    cData = FSO.OpenTextFile("C:\Folder\File.txt").ReadAll
    
    ' split in to array
    arData = Split(cData, vbCrLf)
    
    ' set the first array element to an empty string
    arData(0) = ""
    
    ' create the full data file
    cData = Join(arData, vbCrLf)
    
    ' since the first 2 characters should be the CRLF, do a replace (but only 1)
    cData = Replace(cData, vbCrLf, "", 1, 1, vbBinaryCompare)
    
    ' re-save the text file
    Call FSO.CreateTextFile("C:\Folder\NewFile.txt").Write(cData)
    Set FSO = Nothing
    
    
End Sub

My apologies if my first post caused confusion.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Thats ok...
Guess I am burned out because once you show me the code I feel stupified :)

 
and it worked BTW... thanks once again. As you can see file manipulation is not my strong point.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top