Learn how to a build a cloud-first strategyRegister Now

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 363

Remove Every Other LineBreak in Text File

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
BMIT
• 2
1 Solution

Commented:
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 Commented:
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

Commented:
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

• 2
Tackle projects and never again get stuck behind a technical roadblock.