Jump to content

Recommended Posts

Posted

My question is if for example i write these lines

FileWriteLine($hFileOpen, "Line 1")

FileWriteLine($hFileOpen, "Line 2")

FileWriteLine($hFileOpen, "Line 3")

 

can i after delete them?

Posted (edited)

you can use _FileWriteToLine to delete (i imagine through after readline loop or filereadtoarray function to get the line to delete).

Quote

If _FileWriteToLine() is called with $iOverWrite as 1 and $sText as "", it will delete the line.

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Posted (edited)

Or:

  • calculate the sum of the StringLengths (+ the line breaks)
  • set the position with FileSetPos and the calculated length sum directly ahead of the written lines.
  • use FileSetEnd() to cut the file at this position (the part after the current position is deleted)

as an alternative to the length-calculation you can use FileReadLine by using a negative line number.
So for example if you do FileReadLine($hFileOpen, -4) the file pointer is standing at the end of the fourth last line.
If you now use FileSetEnd() the last three lines will get deleted.

Edited by AspirinJunkie
Posted

Try:

#include <File.au3>

_FileWriteToLine(@ScriptDir & "\test.txt", 3, "my replacement for line 3", True)
_FileWriteToLine(@ScriptDir & "\test.txt", 3, "", False)

Or

#Include <Array.au3>
#include <File.au3>

$sFilePath = @ScriptDir & '\Files.log'
Local $hFileOpen = FileOpen($sFilePath, $FO_APPEND)
If $hFileOpen = -1 Then
    MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")
    Return False
EndIf

FileWriteLine($hFileOpen, "Line 2")
FileWriteLine($hFileOpen, "Line 3" & @CRLF)
FileWriteLine($hFileOpen, "Line 4" & @CRLF)
FileWriteLine($hFileOpen, "Line 5")

$aFiles = FileReadToArray($sFilePath)
$content = FileRead($sFilePath)

MsgBox(0,"Test",$content)

; ===========================
; if you will commented this lines, it will not delete your inserted lines in text file
For $i = 0 to UBound($aFiles)-1
   $content = StringRegExpReplace($content, $aFiles[$i] & '\R?', "")
Next
$hFile = FileOpen($sFilePath, 2)
FileWrite($hFile, $content)
FileClose($hFile)
; ===========================

 

 

 

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

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
  • Recently Browsing   0 members

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