Sign in to follow this  
Followers 0
D4rk^S0ul

Using FileWrite

10 posts in this topic

I was wondering if it was possible to use filewrite to delete something in a file i only need it to delete the last writen letter space or number atm but is that possible?

Share this post


Link to post
Share on other sites



sure, there is probably a better way, but you could do this:

Open the file in read mode and read in the last line.

figure out which character you want to delete and remove it from the variable that holds this line

now open the file in write mode (I'm not sure if you want append or overwrite, I think overwrite is what you want)

then do FileWriteLine() to the last line.

does this make sense? English is my second language..... there was no first language, but english is my second :whistle:

Share this post


Link to post
Share on other sites

The logic is there, but the problem i am having is that i am not sure how to find the last letter writen in the file or the line is.

Share this post


Link to post
Share on other sites

The logic is there, but the problem i am having is that i am not sure how to find the last letter writen in the file or the line is.

#include <File.au3>
$sFilePath = "test.txt"
$lastLine = _FileCountLines( $sFilePath )
$line = FileReadLine($sFilePath, $lastLine)
$newLine = StringTrimRight($line, 1)
_FileWriteToLine($sFilePath, $lastLine, $newLine, 1)

though when I run that code for some reason it seems to insert a newline at the end of the last line.... no characters but it does append a new empty line. hope this helps

Share this post


Link to post
Share on other sites

Thanks for the help, does anyone know how to fix the problem with the extra line? I looked through it but i cant see why this is happening, because i am using FileWrite command to alter the file, when i write after editing it, it types on the new line.

Share this post


Link to post
Share on other sites

Is it just an empty line? If so, StringTrimRight($value, 2) would get rid of a @CRLF at the end.


[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

#7 ·  Posted (edited)

I thought of something like that but what i did had no effect:

#include <File.au3>
$sFilePath = "test.txt"
$lastLine = _FileCountLines( $sFilePath )
$line = FileReadLine($sFilePath, $lastLine)
$newLine = StringTrimRight($line, 1)
_FileWriteToLine($sFilePath, $lastLine, $newLine, 1)

$lastLine = _FileCountLines( $sFilePath )
$line = FileReadLine($sFilePath, $lastLine)
$newLine = StringTrimRight($line, 1)

Im not sure if this was what you were on about.

Edited by D4rk^S0ul

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

$newLine = StringTrimRight($line, 2) << if you are trimming right 1 of a Carriage Return + Line Feed ie... @CRLF, you are only getting rid of the LineFeed, by using "2" you get rid of both.

As you may not know what I'm "on" about, I as well haven't payed as much attention to your post as I should to know your exact issue, I took a stab in the dark that it was writing a blank line, and if it is, then taking that stab would fix your issue :whistle: .

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

I have also tried deleting 2 from the line but it made no difference the extra line still exists even if i try it out on my own made file with a new blank line in it.

Share this post


Link to post
Share on other sites

I think I understand what you are saying...

#include <File.au3>
$sFilePath = @DesktopDir & "\txt.txt"
$lastLine = UBound(StringSplit(StringStripCR(FileRead($sFilePath)), @LF)) - 1
$line = FileReadLine($sFilePath, $lastLine)
_FileWriteToLineDE($sFilePath, $lastLine, $line, 1)

ConsoleWrite(@LF & 'Number of Lines Before: ' & $lastLine & @LF & _
    'Number of Lines After: ' & UBound(StringSplit(StringStripCR(FileRead($sFilePath)), @LF)) - 1 & @LF)

Func _FileWriteToLineDE($sFile, $iLine, $sText, $fOverWrite = 0)
    _FileWriteToLine($sFile, $iLine, $sText, $fOverWrite)
    Local $sReadFile = FileRead($sFile)
    Local $nCount = UBound(StringSplit(StringStripCR($sReadFile), @LF)) - 1
    If FileReadLine($sFile, $nCount) = '' Then
        FileClose(FileOpen($sFile, 2))
        FileWrite($sFile, StringTrimRight($sReadFile, 2))
    EndIf
    Return
EndFunc
And it's what I thought was happening. FileWriteLine does insert a Carriage Return + LineFeed to the end of the file.


[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

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