Jump to content

Trying To Delete Last Line In A File


Recommended Posts

What im trying to accomplish is removing the last line in a file completely. Best way I've come up with using whats available in AutoIt is to copy the entire contents of the file, and write it back without the last line. But I'm sure there is a better way... If I could set which line of the file the WriteLine command would write to, that would be sufficent aswell.

Ooo Ëxçã¿îbúr ooO"Information Is Not Knowledge." ~Albert Einstein
Link to comment
Share on other sites

greenmachine: by executing that statement once, will any future WriteLine commands continue where _FileWriteToLine() left off? Or should I just test it out and see.

Ooo Ëxçã¿îbúr ooO"Information Is Not Knowledge." ~Albert Einstein
Link to comment
Share on other sites

*sighs* oh well, screw it, im doing it my way then... the SLOW way... :think: I'll check this again tommorrow and see if anything good has turned up. Thx for the help.

Ooo Ëxçã¿îbúr ooO"Information Is Not Knowledge." ~Albert Einstein
Link to comment
Share on other sites

  • Moderators

*sighs* oh well, screw it, im doing it my way then... the SLOW way... :think: I'll check this again tommorrow and see if anything good has turned up. Thx for the help.

Don't give up so easy lol... You need beta for this but:
#include <File.Au3>
$FileLocation = @DesktopDir & '\Au3Test.txt'
$TextToWrite = ''
_FileWriteToLine($FileLocation, _FileCountLines($FileLocation), $TextToWrite, 1)
Edit:

Had way too much code for such an easy function, although this does not in fact delete the last line ... only wipes it out :(

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Don't give up so easy lol... You need beta for this but:

#include <File.Au3>
$FileLocation = @DesktopDir & '\Au3Test.txt'
$TextToWrite = ''
_FileWriteToLine($FileLocation, _FileCountLines($FileLocation), $TextToWrite, 1)
Edit:

Had way too much code for such an easy function, although this does not in fact delete the last line ... only wipes it out :think:

To fully delete the line, couldn't you just change the previous line to not have an endline character at the end? It takes two _FileWriteToLine functions - the first to clear the last line, and the second to erase the endline character in the second-to-last line.
Link to comment
Share on other sites

  • Moderators

To fully delete the line, couldn't you just change the previous line to not have an endline character at the end? It takes two _FileWriteToLine functions - the first to clear the last line, and the second to erase the endline character in the second-to-last line.

I decided to just write a UDF for it, it works as far as I can tell:
$FileLocation = @DesktopDir & '\Au3Test.txt'
_FileDeleteLine($FileLocation)


Func _FileDeleteLine($hFile, $i_Last = 0); If other than 0 it will delete whatever line you want
    Local $hFileOpen = '', $aArrayRead = StringSplit(FileRead($hFile, FileGetSize($hFile)), @CRLF, 1)
    $hFileOpen = FileOpen($hFile, 2)
    If $i_Last = 0  And UBound($aArrayRead) - 1 > 0 Then
        For $i_Count = 1 To UBound($aArrayRead) - 2
            If $i_Count = UBound($aArrayRead) - 2 Then
                FileWrite($hFileOpen, $aArrayRead[$i_Count])
            Else
                FileWrite($hFileOpen, $aArrayRead[$i_Count] & @CRLF)
            EndIf
        Next
        FileClose($hFileOpen)
        Return 1
    ElseIf $i_Last <> 0 And UBound($aArrayRead) - 1 > 0 Then
        For $i_Count = 1 To UBound($aArrayRead) - 1
            If $i_Count = $i_Last Then ContinueLoop
            If $i_Count = UBound($aArrayRead) - 1 Then
                FileWrite($hFileOpen, $aArrayRead[$i_Count])
            Else
                FileWrite($hFileOpen, $aArrayRead[$i_Count] & @CRLF)
            EndIf
        Next
        FileClose($hFileOpen)
        Return 1
    EndIf
    FileClose($hFileOpen)
    Return 0
EndFunc
You can leave the last parameter empty and it will delete the last line, or you can put a number in and it will delete that line specifically.

Edit:

I just noticed I had a variable in there that wasn't being used.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

@Excalibur - Did this work for you?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

using array pop

#include <file.au3>
#include <Array.au3>
Dim $aRecords
If Not _FileReadToArray("test.txt",$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf

MsgBox(0,'Line removed',_ArrayPop($aRecords))
_ArrayDisplay( $aRecords, "Entries left in the array" )

8)

NEWHeader1.png

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...