Sign in to follow this  
Followers 0
Excalibur

Trying To Delete Last Line In A File

13 posts in this topic

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

Share this post


Link to post
Share on other sites



check out

Filecountlines

Share this post


Link to post
Share on other sites

_FileWriteToLine() in the beta (somewhere around .100?). Latest will definitely have it.

Share this post


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

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

no :think:, you specify the line to right to so yeah....

Edited by thatsgreat2345

Share this post


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

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

*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

[center]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.[/center]

Share this post


Link to post
Share on other sites

Thanks smoke, but I've concidered your solution, what it would do is leave a return on that line causing skips in the text file.


Ooo Ëxçã¿îbúr ooO"Information Is Not Knowledge." ~Albert Einstein

Share this post


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

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

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

[center]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.[/center]

Share this post


Link to post
Share on other sites

#11 ·  Posted

@Excalibur - Did this work for you?


[center]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.[/center]

Share this post


Link to post
Share on other sites

#12 ·  Posted

It worked for me when I tested it. :) Good job smoke.

Share this post


Link to post
Share on other sites

#13 ·  Posted

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

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