VBScript: Flip Line Endings

Recently I was working for a client who had particularly strict controls around access and tools that I could use. Whilst the bulk of the work I was carrying out on their Linux box could be managed using vi, I found it a little limiting when analysing some of the log files the work was producing. On the Windows machine I was using to access the files I only had Windows notepad and this really doesn’t handle Linux file endings well. As a result I put together a quick VBScript to help with this.

Whilst there are often tools available on the Linux side to do line ending conversions I was unable to find any in this case and since I was unable to get a better text editor installed on the Windows PC it seemed my chances of putting new software on to the Linux server was even less likely. Hence my option to quickly put together a VBScript to do the job for me.

The issue is that on Linux file, the line feed character is used on its own to denote the end of a line whereas on Windows, the carriage return and line feed character combination is used to denote the end of a line. The script I created therefore just added on the extra carriage returns I needed.

As the work progressed there were a few occasions where I needed to do the reverse so I created a second script to support translating carriage return and line feeds into just line feeds. Since that work completed I decided to amalgamate and tidy the scripts into a single script an share it just in case anyone else might have need of it.

The basic premise remains a simple find and replace, but the script below is configured to accept multiple files (drag and drop, or send-to/command line parameters) and convert them based on the type of file. If it finds any carriage return and line feed combinations it will convert to just line feeds, otherwise it will add in the requisite carriage returns.

The script includes a constant named CONS_RUNSILENTLY. Setting this to False will display message boxes when a conversion completes. Setting it to True (the default) will skip displaying those message boxes. There are also a couple of helper functions towards the bottom that set the silent running parameter of the main function to enable you to more easily use this functionality in your own scripts.

Option Explicit

'Set to True to run without any message box notifications
Const CONS_RUNSILENTLY = True

Main

Sub Main()
    Dim astrScriptParameters
    Dim intCounter
    'Get the command line parameters received by the script
    Set astrScriptParameters = Wscript.Arguments


    If astrScriptParameters.Count > 0 Then
        'Each parameter should be a path for a plain text based file.
        '(Process each argument as though it is)
        For intCounter = 0 to astrScriptParameters.Count - 1
            FlipLineEndings Wscript.Arguments(intCounter), NOT(CONS_RUNSILENTLY)
        Next
        
    Else
        'If there's not even one argument/file to process then exit
        Msgbox "Please pass a file to this script", 48,"No File Provided"
        WScript.Quit
    End If
End Sub


'Changes Windows line endings to UNIX and vice versa
Function FlipLineEndings(p_strFilePath, p_bDisplayMsgBox)
    Dim strContent, strMessage
    Dim objFSO, objFile

    'File access constants
    Const CON_FORREADING = 1
    Const CON_FORWRITING = 2

    'Title for message boxes
    Const CON_MSGBOXTITLE = "Line Conversion"

    'Create our object for dealing with the file system
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    'Check path passed in exists
    If objFSO.FileExists(p_strFilePath) then

        'Read in the file content
        Set objFile = objFSO.OpenTextFile(p_strFilePath, CON_FORREADING)
        strContent = objFile.ReadAll
        objFile.Close

        'Does it contain CrLfs?
        If InStr(1, strContent, vbCrLf, vbTextCompare) > 0 Then
            'Change Windows line endings to UNIX line endings
            strContent = Replace(strContent, vbCrLf, vbLf)
            'Match any output message we might need to display
            strMessage = "Windows (CrLf) to UNIX (Lf)."
        Else
            'Change UNIX line endings to Windows line endings
            strContent = Replace(strContent, vbLf, vbCrLf)
            'Match any output message we might need to display
            strMessage = "UNIX (Lf) to Windows (CrLf)."
        End If

        'Delete original file
        objFSO.DeleteFile p_strFilePath

        'Write file
        Set objFile = objFSO.OpenTextFile(p_strFilePath, CON_FORWRITING, True, False)
        objFile.Write(strContent)

        'Output success
        If p_bDisplayMsgBox then MsgBox strMessage & vbCrLf & p_strFilePath, vbOKOnly + vbInformation, CON_MSGBOXTITLE

        'Return success
        FlipLineEndings = True
    Else
        'Output failure
        If p_bDisplayMsgBox then MsgBox "File Not Found" & vbCrLf & p_strFilePath, vbOKOnly + VbCritical, CON_MSGBOXTITLE

        'Return failure
        FlipLineEndings = False
    End If
End Function

'Helper function if this is included elsewhere - run with notification
Function FlipLineEndingsNotify(p_strFilePath)
    FlipLineEndingsNotify = FlipLineEndings(p_strFilePath, True)
End Function

'Helper function if this is included elsewhere - run silently
Function FlipLineEndingsSilent(p_strFilePath)
    FlipLineEndingsSilent = FlipLineEndings(p_strFilePath, False)
End Function

This was a bit of an unusual case in that I didn’t have access to my usual tools or have a way to easily access the files on my own work laptop. However in my experience if an unusual case pops up once there’s a good chance it will pop up again for someone, somewhere, some time. If you don’t have a better means available to you then perhaps this might be just the script your looking for.

Author: Stephen Millard
Tags: | vbs |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read