Rand
Rand

Reputation: 53

how can I delete blank lines at the end of text file using VBScript?

below my text file:

"LIRRR 1M",.412900,02/08/2016
"LIRRR 3M",.222700,02/08/2016
"10YRCMT",1.860000,02/08/2016

I have blanks lines at the end of my text file . I want to clean my file from the blank . added the code I used but my code have not remove the blank at the end of the file below the code I have created :

Const ForReading = 1

Const ForWriting = 2


Set objFSO = CreateObject(“Scripting.FileSystemObject”)

Set objFile = objFSO.OpenTextFile(“C:\Users\Desktop\Test.txt”, ForReading)


Do Until objFile.AtEndOfStream

    strLine = objFile.Readline

    strLine = Trim(strLine)

    If Len(strLine) > 0 Then

        strNewContents = strNewContents & strLine & vbCrLf

    End If

Loop
objFile.Close


Set objFile = objFSO.OpenTextFile(“C:\Users\Desktop\Test2.txt”, ForWriting)

objFile.Write strNewContents

objFile.Close

Upvotes: 1

Views: 3938

Answers (1)

Have you tried "trim" function? Here is the reference: http://www.w3schools.com/asp/func_trim.asp

Edit: Trim works in a string. If your file is not too big, you could read it in a string, trim it and write into file again. One possible cause of these blanking spaces on your file could be because you're writing your files, i.e., adding strings, without trimming them. Another aproach would be putting a marker at the begining of the blank space, then split file at that point.

Upvotes: 1

Related Questions