Right-trimming text files below vbscript code took me 3hours just to complete trimming whitespaces of a 24MB file any tips to speed up the process below?


dim filename, path \'variables
     Set objFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)
     Set objFile = objFSO.GetFile(Wscript.ScriptFullName)
     strFolder = objFSO.GetParentFolderName(objFile)
     filename = replace(strFolder,&quot;jobs\SCRIPTS&quot;,&quot;shared\file&quot;) &amp; &quot;\DCS.txt&quot;
     converter(filename)
     objFile.Close
     function converter(filename)
     Const ForReading = 1
     Const ForWriting = 2
     Set objFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)
     PATH = filename
     Set objFile = objFSO.OpenTextFile(PATH, ForReading)
     Do Until objFile.AtEndOfStream
        strLine = objFile.Readline
        strLine = Trim(strLine)
        If Len(strLine)  0 Then
            strNewContents = strNewContents &amp; strLine &amp; vbcrlf
        End If
     Loop
     objFile.Close
     Set objFile = objFSO.OpenTextFile(PATH, ForWriting)
     objFile.Write strNewContents
     objFile.Close
     msgbox &quot;done trim on &quot; &amp; filename
     end function
If you're going to be writing VBScript or WSScript files then you should be using VBEdit.


Allow it is not required you should still capitalize End, Function and Sub.
Wrapping each tasks in it's own Sub or Function will make it easier to read, debug and modify your code.
A Function that does not return a value should be a Sub.
Avoid editing text files line by line.  It is much more efficient to split the text into an array of lines, edit each element of the array, and then join the output.
Refactored Code
This code takes less than 1 second to trim each line of a 27 MB text file.
Dim FullFileName
FullFileName = GetSharedFileName

If FileSystemObject.FileExists(FullFileName) Then
    TrimFileContents FullFileName
    MsgBox &quot;Done&quot;
Else
    MsgBox &quot;File not found: &quot; &amp; FullFileName
End If


Sub TrimFileContents(FullFileName)
    Dim Lines
    Lines = GetTextFileLines(FullFileName)
    
    Dim n

    For n = 0 To UBound(Lines)
        Lines(n) = Trim(Lines(n))
    Next
    
    OverWriteTextFile FullFileName, Join(Lines, vbNewLine)
End Sub

Function GetSharedFileName
    GetSharedFileName = Replace(GetParentFolderName,&quot;jobs\SCRIPTS&quot;,&quot;shared\file&quot;) &amp; &quot;\DCS.txt&quot;
End Function

Function FileSystemObject
    Set FileSystemObject = CreateObject(&quot;Scripting.FileSystemObject&quot;)
End Function


Function GetParentFolderName

    GetParentFolderName = FileSystemObject.GetFile(Wscript.ScriptFullName).ParentFolder.Path

End Function

Function GetTextFileLines(FullFileName)
    Const ForReading = 1
    Dim Text
    With FileSystemObject.OpenTextFile(FullFileName, ForReading)
        Text = .ReadAll
        .Close
    End With
    
    If InStr(Text, vbNewLine) = 0 Then
        GetTextFileLines = Split(Text, vbLf)
    Else
        GetTextFileLines = Split(Text, vbNewLine)
    End If

End Function

Sub OverWriteTextFile(FullFileName, Text)
    Const ForWriting = 2

    With FileSystemObject.OpenTextFile(FullFileName, ForWriting)
        .Write Text
        .Close
    End With
    
End Sub