Announcement

Collapse
No announcement yet.

VBScript: rTrim every line of a text file

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • VBScript: rTrim every line of a text file

    As part of another script, I have the need to clean up all trailing spaces from all lines of a text file (rTrim). Below is what I have come up with and it works a treat but I'm sure there must be a better more efficient way to do it. Any ideas?

    Code:
    Option Explicit
    
    Const ForReading = 1
    Const ForWriting = 2
    Const Open_as_Unicode = True
    
    Dim objFSO, objFile
    Dim strFolder, strFile, strNewLine, strFile, strLine
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    strFolder = "C:\Files"
    strFile = "\File.txt"
    strFile = strFolder & strFile
    
    Set objTNS = objFSO.OpenTextFile(strFile, ForReading)
    
    Do Until objFile.AtEndOfStream
        strLine = objFile.ReadLine
        strLine = rTrim(strLine)
        strNewLine = strNewLine & strLine & vbCrlf
    Loop
    objFile.Close
    
    Set objFile = objFSO.OpenTextFile(strFile, ForWriting)
    objFile.Write strNewLine
    objFile.Close
    
    WScript.Quit
Working...
X