Jump to content

Remove last line in Text file


Gilly
 Share

Recommended Posts

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
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...

Link to comment
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...

Link to comment
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

Link to comment
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

Link to comment
Share on other sites

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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...