none
Converting new line sequence doesn't change last line RRS feed

  • Question

  • I'm using a small script to convert the new line sequence between unix/mac/windows formats. It works fine for every line of text in the file, except the last line. I can change from any to any (CR, LF, CRLF), but the last line always has CRLF, no matter what sequence is specified on the command line.

    Brief description: I accept 3 command line arguments, physical folder of the file, filename, and desired new line sequence. I specify the folder and filename seaerately because I use a temp location with the filename, then put it back in the original place. Any help would be greatly appreciated! TIA

    Usage: <script>.ps1 <folder> <file> [newline]
    folder        : folder where file to convert exists
    file        : file to convert newline sequence
    newline        : newline sequence to convert to (CR, LF, CRLF)
            : no newline sequence specified defaults to CRLF

    Below is my script:
    -------------
    # Populate working variables with specified command line arguments
    $sInFolder = $args[0]
    $sInFile = $args[1]
    switch ($args[2])
    {
            "CR"        {$sNewLine = "`r"}
            "LF"        {$sNewLine = "`n"}
            "CRLF"        {$sNewLine = "`r`n"}
            Default {$sNewLine = "`r`n"}
    }

    $sFileToProcess = "$sInFolder\$sInFile"
    $sTempFile="$env:TEMP\$sInFile.tmp"

    # Convert new line sequence from original to specifed sequence
    Set-Content $sTempFile $([string]::Join( $sNewLine, (Get-Content $sFileToProcess)))
    copy-item $sTempFile $sFileToProcess

    # Clean up temp workspace
    remove-item $sTempFile
    -------------

    • Edited by CERTX64 Thursday, August 29, 2013 7:40 PM
    Thursday, August 29, 2013 7:39 PM