Remove Every Other LineBreak in Text File

Posted on 2011-10-18
Last Modified: 2012-05-12
I think I'm just drawing a blank here, but I can't seem to figure out how to remove a linebreak more specifically EVERY OTHER LINEBREAK from txt file using VBScript.

Here is the code I have to load the file into and array in order for my program to have control over it, I know it works because the little bit of code at the end will read back the file to me correctly.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\W2Project\W2REPORT.txt", ForReading)

Const ForReading = 1

Dim arrFileLines()
i = 0
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1

'Then you can iterate it like this

For Each strLine in arrFileLines

WScript.Echo strLine


Open in new window

What I need done is to make line 1 and 2 into one line, then make line 3 and 4 into one line, then line 5 and 6 into one line, etc.

Not sure if the array approach is best or if reading it line for line or character by character is best.

Any suggestions?

Question by:BMIT
    LVL 51

    Accepted Solution

    Here's an approach, and shows an easier way to load the whole file into an array.  It then iterates through the lines two at a time, writing the concatenation of each pair out as a single line with a space between.  The IF logic in the loop is incase there is an odd number of lines, since we need to write out just the last line as it is in that case.

    Const ForReading = 1
    Const ForWriting = 2
    Const TriStateUseDefault = -2
    strInput = "C:\temp\EE26169735\in.txt"
    strOutput = "C:\temp\EE26169735\out.txt"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objInput = objFSO.OpenTextFile(strInput, ForReading, False, TriStateUseDefault)
    arrLines = Split(objInput.ReadAll, vbCrLf)
    Set objOutput = objFSO.OpenTextFile(strOutput, ForWriting, True)
    For i = 0 To UBound(arrLines) Step 2
       If i+1 > UBound(arrLines) Then
          objOutput.WriteLine arrLines(i)
          objOutput.WriteLine arrLines(i) & " " & arrLines(i+1)
       End If

    Open in new window


    Author Closing Comment

    That is eloquent coding.  I completely forgot about the SPLIT command, I've used it before to build an array from text.

    I will be removing the space between the concatenated lines, but otherwise this worked beautifully.

    LVL 51

    Expert Comment

    by:Bill Prew
    Great, glad that helped.  Understand on space, I wasn't sure if it was desired or not so took a shot and put it in.  Thanks for the feedback.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (…
    Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  ( Here (http…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    759 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

    10 Experts available now in Live!

    Get 1:1 Help Now