Jump to content

Using FileWrite


Recommended Posts

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:

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

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

Link to comment
Share on other sites

  • Moderators

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

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

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

  • Moderators

$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

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

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.

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

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