Jump to content
Sign in to follow this  
PcExpert

File Delete Line

Recommended Posts

not a built-in function that i know of, but i suppose you could _FileReadToArray() and then _ArrayDelete() the line you want to remove.

Share this post


Link to post
Share on other sites

Func _Delete_Line_In_File($fileName, $lineNum)
    
    Local $len = FileGetSize($fileName)
    Local $txt = FileRead($fileName, $len)
    If @error = 1 Then Return 0
    Local $pos1 = StringInStr($txt, @lf, 0, $lineNum - 1)
    Local $pos2 = StringInStr($txt, @lf, 0, $lineNum)
    If $pos1 > 0 Or $pos2 > 0 Then
        If $pos2 = 0 Then $pos2 = $len
        FileDelete($fileName)
        FileWrite($fileName, StringMid($txt, 1, $pos1) & StringMid($txt, $pos2 + 1))
        Return 1
    EndIf
    Return 0
   
EndFunc ;==> _Delete_Line_In_File()

Share this post


Link to post
Share on other sites

Hi all,

Is there a function in autoit to delete a specified line?

Thanks!

More solutions :)

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

Global $StrArray[1]

DeleteString(@ScriptDir & "\test.txt", 3)

Func DeleteString($sFileName, $sLine)
    Local $FileHwnd
    
    If Not FileExists($sFileName) Then
        MsgBox(16, "Error", "File not exist")
        Exit
    EndIf
    
    _FileReadToArray($sFileName, $StrArray)
    _ArrayDelete($StrArray, $sLine)
    ;_ArrayDisplay($StrArray)
    
    $FileHwnd = FileOpen($sFileName, 2)
    
    For $i = 1 To UBound($StrArray) -1
        FileWriteLine($FileHwnd, $StrArray[$i])
    Next
    
    FileClose($FileHwnd)
EndFunc

Share this post


Link to post
Share on other sites

Small note, I dont recommend using arrays. Array is slow as hell. Especially if you have files with around 100 000 lines in them or you simply phrise many files. String thing is alot faster.

Share this post


Link to post
Share on other sites

_FileWriteToLine

Remarks

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



Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Share this post


Link to post
Share on other sites

@Big Dod

How to use that function if I dont know wich line I want to delete, I know the text, but not the line number.

Share this post


Link to post
Share on other sites

This is an old post, but if you attempted to use _FileWriteToLine() recently and for any reason it could not open, NeuroToxic's solution works like a charm. Simply put it into a for loop using FileReadLine to find your line number with the matching string and then use the _Delete_Line_In_File() function.

_FileWriteToLine() could not open my file, regardless how I gave it the file, closing it, opening it, etc.

_ReplaceStringInFile() wasn't a very good choice either for what I was doing.

I'll hold on to NeuroToxic's little code snippet for sure. Adding

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  

×
×
  • Create New...