How to right trim all lines in a textfile?

Download input test file: 20100224.HowTo.RightTrimAllLines.Example.in.txt
;==========================================================================================================================================
;
; How to right trim all lines in a textfile?
;
; Two example scripts which demonstrate right trimming using different methods.
;
;
; (c)Detlev Dalitz.20100224.
;==========================================================================================================================================

GoSub Example1
GoSub Example2

:CANCEL
Exit


;---;
; 1 ;
;---+--------------------------------------------------------------------------------------------------------------------------------------
; Right trimming all lines in textfile (StrReplace version).
;
; (c)Detlev Dalitz.20090317.
;------------------------------------------------------------------------------------------------------------------------------------------
:Example1

DirChange (DirScript ())

; Input file is a clean textfile with each line terminated by CRLF sequence (even the last line).
; Filesize should not be too big.
strFileIn = ".\20100224.HowTo.RightTrimAllLines.Example.in.txt"     ; Use your own inputfile.
strFileOut = ".\20100224.HowTo.RightTrimAllLines.Example.out.1.txt" ; Use your own outputfile.
If FileExist (strFileOut) == 1 Then FileDelete (strFileOut)

; Read textfile into text variable.
strText = FileGet (strFileIn)

; Calculate magic value for the following loop.
strItem = ItemExtract (1, strText, @CR)
intFill = Min (512, StrLen (strItem) - StrLen (StrReplace (strItem, " ", "")))

; Shrink all lines step by step in a loop.
While intFill > 0
   strSearch = StrFill (" ", intFill) : @CR
   If 0 < StrIndex (strText, strSearch, 0, @BACKSCAN)
      strText = StrReplace (strText, strSearch, @CR)
   Else
      intFill = intFill / 2
   EndIf
EndWhile

; Write out text variable to textfile.
intResult = FilePut (strFileOut, strText)


;------------------------------------------------------------------------------------------------------------------------------------------
; Display result.
;------------------------------------------------------------------------------------------------------------------------------------------
; Message.
strMsgTitle = "Big File Repair - Result"
strMsgText = "Filesize In: " : FileSize (strFileIn) : " Bytes" : @LF : "Filesize Out: " : FileSize (strFileOut) : " Bytes"
ClipPut (strMsgTitle : @LF : strMsgText)
Pause (strMsgTitle, strMsgText)

; File content.
If FileExist (strFileOut) != 0 Then Run (DirHome () : "Browser.exe", strFileOut)

; Big File Repair - Result
; Filesize In: 10935 Bytes
; Filesize Out: 6734 Bytes

Return



;---;
; 2 ;
;---+--------------------------------------------------------------------------------------------------------------------------------------
; Right trimming all lines in textfile (LogParser version).
;
; (c)Detlev Dalitz.20081021.
;------------------------------------------------------------------------------------------------------------------------------------------
:Example2

DirChange (DirScript ())

;------------------------------------------------------------------------------------------------------------------------------------------
; Trim Big File.
;------------------------------------------------------------------------------------------------------------------------------------------
; Define path to input and output files.
strFileIn = ".\20100224.HowTo.RightTrimAllLines.Example.in.txt"     ; Use your own inputfile.
strFileOut = ".\20100224.HowTo.RightTrimAllLines.Example.out.2.txt" ; Use your own outputfile.
If FileExist (strFileOut) == 1 Then FileDelete (strFileOut)

; Define path to LogParser application, needs to be installed.
strApp = "P:\Programs\LogParser\LogParser.exe"

; Right trim all lines.
strParams = ` -q:ON -i:TEXTLINE -o:NAT "SELECT RTRIM(TEXT) INTO ` : strFileOut : ` FROM ` : strFileIn : `"`
intResult = RunShell (strApp, strParams, "", @HIDDEN, @WAIT) ; @ZOOMED while debugging.


;------------------------------------------------------------------------------------------------------------------------------------------
; Display result.
;------------------------------------------------------------------------------------------------------------------------------------------
; Message.
strMsgTitle = "Big File Repair - Result"
strMsgText = "Filesize In: " : FileSize (strFileIn) : " Bytes" : @LF : "Filesize Out: " : FileSize (strFileOut) : " Bytes"
ClipPut (strMsgTitle : @LF : strMsgText)
Pause (strMsgTitle, strMsgText)

; File content.
If FileExist (strFileOut) != 0 Then Run (DirHome () : "Browser.exe", strFileOut)

; Big File Repair - Result
; Filesize In: 10935 Bytes
; Filesize Out: 6734 Bytes

Return
;==========================================================================================================================================