Sign in to follow this  
Followers 0
cheesestain

Delete Empty LInes

5 posts in this topic

How can I delete empty lines at the end of a text file?

I tried to do a search for @LF and replace with "". That doesn't seem to work.

Share this post


Link to post
Share on other sites



Usually the "empty" lines are @CRLF, not only @LF.


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I can't seem to get this to work, I tried reading in the number of lines minus the blank lines but it still outputs a blank line.

"The line of text to write to the text file. If the line does NOT end in @CR or @LF then a DOS linefeed (@CRLF) will be automatically added."

How can I prevent it from doing this on the last line?

If I try to replace @CRLF it stips all the carriage returns. Sigh.

Edited by cheesestain

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Try with this RegExReplace:

$Output = 'James Smith Jamie Brown' & @LF
$Output &= 'Philly' & @LF
$Output &= 'Joe' & @LF
$Output &= 'Blip' & @LF
$Output &= @LF
$Output &= 'paul' & @LF
$Output &= @LF
$Output &= @LF
$Output &= @LF
$Output &= 'tad' & @LF
$Output &= @LF
$Output &= @LF
$Output &= @LF


MsgBox(32, 'Before', StringReplace($Output, @LF, '<CR>' & @LF)) ;Added "<CR>" to show where a LineFeed is placed

$Output = StringRegExpReplace($Output, '[\s]*$|([\n\r]){2,}', '$1') ;Trims consecutive line feeds anywhere in string and all Whitespaces after non-whitespaces 

MsgBox(32, 'After', StringReplace($Output, @LF, '<CR>' & @LF))  ;Added "<CR>" to show where a LineFeed is placed
Or in a script accepting file input:
#include-once
#include <Array.au3>

If $CmdLine[0] = 0 Then
    $List = _GetFiles('Supported Files (*.txt;*.ini;*.doc;*.nfo;*.au3;*.reg)')
Else
    $List = $CmdLine
EndIf

For $i = 1 To $List[0]
    $String = FileRead($List[$i])
    FileOpen($List[$i], 2)
    $String = StringRegExpReplace($String, '[\s]*$|([\n\r]){2,}', '$1') ;Trims Whitespaces at the end of string
    FileWrite($List[$i], $String)
Next

Func _GetFiles($Type = 'All Files (*.*)', $InitDir = '')
    Local $String, $ConversionList, $Flag, $oFile
    If $CmdLine[0] = 0 Then
        $Message = 'Choose the File(s) to Open.'
        $SourceFile = FileOpenDialog($Message, $InitDir, $Type, _GetIEVersion())
        If @error Then Exit
        If StringInStr($SourceFile, '|') Then
            $ConversionList = StringSplit($SourceFile, '|', @CRLF)
            For $i = 2 To $ConversionList[0]
                $ConversionList[$i] = $ConversionList[1] & '\' & $ConversionList[$i]
            Next
            _ArrayDelete($ConversionList, 1)
            $ConversionList[0] = UBound($ConversionList) - 1
        Else
            Local $ConversionList[2] = [1, $SourceFile]
        EndIf
    ElseIf $CmdLine[0] > 1 Then
        $CmdLineRaw = StringReplace(StringReplace($CmdLineRaw, '" "', '","'), '"', '')
        $ConversionList = StringSplit($CmdLineRaw, ',')
    Else
        $ConversionList = $CmdLine
    EndIf
    Return $ConversionList
EndFunc   ;==>_GetFiles

Func _GetIEVersion()
    $iE_Version = FileGetVersion(@ProgramFilesDir & '\Internet Explorer\iexplore.exe')
    Switch Int(FileGetVersion(@ProgramFilesDir & '\Internet Explorer\iexplore.exe'))
        Case 0 To 4
            Global $Flag = 4
        Case 5
            Global $Flag = 2 + 4
        Case Else
            Global $Flag = 1 + 2 + 4
    EndSwitch
    Return $Flag
EndFunc   ;==>_GetIEVersion

Edited by Varian

Share this post


Link to post
Share on other sites

Thanks, your first example worked!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0