Solved

# Remove Every Other LineBreak in Text File

Posted on 2011-10-18
316 Views
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")

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

'Then you can iterate it like this

For Each strLine in arrFileLines

WScript.Echo strLine

Next


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?

Thanks,
0
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)
objInput.Close

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)
Else
objOutput.WriteLine arrLines(i) & " " & arrLines(i+1)
End If
Next

objOutput.Close

~bp
0

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.

Thanks,
0

LVL 51

Expert Comment

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.

~bp
0

## Featured Post

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 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
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  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) 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 (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…