Jump to content

File Delete Line


Recommended Posts

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()

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

My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]
Link to comment
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

Link to comment
Share on other sites

  • 4 years later...

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

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