Sign in to follow this  
Followers 0
Gilly

Remove last line in Text file

14 posts in this topic

Hopefully someone will be able to let me know what I am doing wrong, or at least let me know how to get around it.

I have a text file which consists of one word per line, and occassionally will contain an empty line--just a carriage return to the next line. I am trying to remove those empty lines. In other words:

line1
line2

line4

I want to become:

line1
line2
line4

I am using a function which I downloaded from the forum called _removeLineInFile. It uses _FileReadToArray and all is good unless the empty line is the last line of the text file--if that is the case...it doesn't even load that line into the array, even though that line DOES exist...In this case, the line after 'line4' won't get loaded into the array, which, I am guessing, is why it doesn't remove that line.

Any ideas on how I can remove that line?

Thanks,

Joe

Share this post


Link to post
Share on other sites



$handle=FileOpen("file")

Dim $a

while 1

$a=FileReadLine($handle)

if $a=@cr or $a=@lf or $a=@crlf then

...

endif

wend

Share this post


Link to post
Share on other sites

Sorry, my last answer was wrong

In a file

Line1&@cr

Line2&@cr

@cr

Line4

if in the last line there is a @cr

Line1&@cr

Line2&@cr

@cr

Line4&@cr

you see a carriage return in your "notepad"

but @cr isn't in the "Line5"

is at the end of the "Line4" :)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Const $filename = "C:\test.txt"; Change location and name to your actual file
Local $file, $line, $output
$file = FileOpen($filename, 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    If $line <> @CRLF AND $line <> @CR AND $line <> @LF Then
       $output = $output & @CRLF & $line
    EndIf
Wend
FileClose($file)

$file = FileOpen($filename, 2); Write mode erase previous contents

; Check if file opened for writing OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

FileWrite($file, $output)
FileClose($file)

Note: Edited to change write mode from Append (1) to Erase (2).

Edited by DaRam

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I was trying to recreate how the file looked on the web page--but the formatting kept taking away the space at the end of my example The file would look like this:

---start of file -----

line1

line2

line4

-----end of file------

In other words--there are 2 empty lines, one before line4 and one after line4. PLUS, when I load the file in Scite--it shows a CR LF...not just a CR--don't know if that changes anything.

Thanks again.

Edited by Gilly

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Dim $sText = FileRead(@ScriptDir & '\123.txt')

$sText = StringRegExpReplace($sText, '(?m)^(\r\n)', '')

Dim $hFile = FileOpen(@ScriptDir & '\123.txt', 2)
FileWrite($hFile, $sText)
FileClose($hFile)

Edit: oh.. lol my mistake, use StringSplit($sTxt, @CRLF, 1) and remove the last line that contain only @CRLF using _ArrayDelete().

Edited by Authenticity

Share this post


Link to post
Share on other sites

Sorry, for any misunderstanding...the last line doesn't contain only a @CRLF...Here it is according to Scite:

-----start of file---------

line1|CRLF

line2|CRLF

CRLF

line4|CRLF

-----end of file----

Really line 5 doesn't show anything, not even a CRLF, but it's there...the trick is how do I get rid of that line? Sorry for any confusion...

Share this post


Link to post
Share on other sites

It can be vertical tab, line-feed, carriage-return, or any Unicode space characters. If you have hex viewer or something similar you can check this byte(s) to see what it's value and compare it in the charmap.exe provided by Microsoft (if you like).

Share this post


Link to post
Share on other sites

Did you try the code I posted in Post #4 ?

Sorry, for any misunderstanding...the last line doesn't contain only a @CRLF...Here it is according to Scite:

-----start of file---------

line1|CRLF

line2|CRLF

CRLF

line4|CRLF

-----end of file----

Really line 5 doesn't show anything, not even a CRLF, but it's there...the trick is how do I get rid of that line? Sorry for any confusion...

Share this post


Link to post
Share on other sites

It SEEMS to be working, however, it appends it to the end of the file...that should be a quick tweak, right?

Thanks,

Joe

Share this post


Link to post
Share on other sites

Yes, that was left while I was checking to ensure I was not loosing contents.

Please change the , 1 in FileOpen to , 2 so it will erase previous contents while opening.

I have modified the code in my post accordingly.

It SEEMS to be working, however, it appends it to the end of the file...that should be a quick tweak, right?

Thanks,

Joe

Share this post


Link to post
Share on other sites

OK, I've done that...and it's taking away the space at the end and putting it at the end...at least I can remove those spaces with my _removeLineInFile function...

So the plan will be to run this section of code and then run the _removeLineInFile function--sound good?

Thanks again,

Joe

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

ARGH!!!!

After I run the _removeLineInFile function...it re-adds the line at the end. Here is the function:

Func _removeLineInFile($pathOld, $pathNew, $search, $opt = 1)
;===============================================================================
;
; Function Name:   _removeLineInFile
; Description:: _removeLineInFile
; Parameter(s): Opt = 1 delete line if matches $search
;              Opt = 2 Line starts with
;              Opt = 3 Line contains
; Requirement(s):  #include <file.au3> and #include <Array.au3>
; Return Value(s):  1 success
; Return Value(s): -1 file not found
; Return Value(s): -2 invalid path to write
; Author(s):       th.meger
;
;===============================================================================

Local $FileOne
Local $FileTwo[1]
    
If Not _FileReadToArray($pathOld, $FileOne) Then Return -1

For $i = 1 To $FileOne[0]
    Switch $opt
        Case 1
            If $FileOne[$i] <> $search Then _ArrayAdd($FileTwo, $FileOne[$i])
        Case 2
            If StringLeft($FileOne[$i], StringLen($search)) <> $search Then _ArrayAdd($FileTwo, $FileOne[$i])
        Case 3
            If StringInStr($FileOne[$i], $search) = 0 Then _ArrayAdd($FileTwo, $FileOne[$i])
    EndSwitch
Next
_FileWriteFromArray($pathNew, $FileTwo, 1)
If @error = 0 Then Return 1
Return -2
EndFunc ;==>_removeLineInFile

Any ideas? Even if I just modify the function so the line doesn't show up at the end?

Edited by Gilly

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

UPDATE:

I went back to an older version of AutoIt and all is well again...

Edited by Gilly

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