_removeLineInFile_UDF

    • Offizieller Beitrag

    Hi,

    ich habe mal versucht, das Löschen in Dateien zu vereinfachen.

    Dies ist der erste Wurf, also bitte nicht gleich den Kopf abreißen.

    Vielen Dank im Voraus für die Kritik.

    Spoiler anzeigen
    [autoit]

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

    [/autoit] [autoit][/autoit] [autoit]

    Global $path = "test.txt"
    Global $NewPath = "test1.txt"
    Global $search = "Autoit"

    [/autoit] [autoit][/autoit] [autoit]

    ;$h_path, $s_search, $i_opt = 1, $i_caseSensitive = 0, $i_count = 0, $i_topDown = 0, $h_pathNew = "", $i_inEx = 0

    [/autoit] [autoit][/autoit] [autoit]

    ; keep last 2 lines starting with "Autoit" not case sensitive searching bottomUp
    _removeLineInFile($path, $search, 2, 0, 5, 1, "", 1)
    ; keep all lines containing "Autoit"
    _removeLineInFile($path, $search, 4, 0, 0, 0, "", 1)
    ; deletes all lines = "Autoit" case sensitive
    _removeLineInFile($path, $search, 1, 1)
    ; deletes the first 2 lines = "Autoit" not case sensitive searching bottomUp
    _removeLineInFile($path, $search, 1, 0, 2, 1)
    ; deletes all lines containing the string "Autoit"
    _removeLineInFile($path, $search, 4)
    ; deletes the first line containing the string "Autoit" searching bottomUp
    _removeLineInFile($path, $search, 4, 0, 1, 1)
    ; deletes the first 5 lines starting with "Autoit" case sensitive, searching topDown and save the result in $newPath
    _removeLineInFile($path, $search, 4, 1, 5, 0, $NewPath)
    ; deletes all lines ending with "Autoit" case sensitive, searching topDown and save the result in $newPath
    _removeLineInFile($path, $search, 3, 1, 0, 0, $NewPath)

    [/autoit] [autoit][/autoit] [autoit]

    MsgBox(64, "To see the return value", _removeLineInFile($path, $search, 3, 0, 0, 1)) ; display the return value

    [/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ;
    ; Function Name: _removeLineInFile
    ; Description:: _removeLineInFile
    ; Parameter(s):
    ; 1. $h_path = Path to file
    ; 2. $s_search = string pattern
    ; 3. Opt = 1 Delete/keep line if matches $s_search
    ; Opt = 2 Delete/keep line if starts with $s_search
    ; Opt = 3 Delete/keep line if ends with $s_search
    ; Opt = 4 Delete/keep line if contains $s_search
    ; 4. 0 = not case sensitive (default)
    ; 1 = case sensitive
    ; 5. $i_count = How many occurrances should be deleted
    ; 6. $i_TopDown = 0 (default) search --> 1-end
    ; $i_TopDown = 1 search bottumUp --> end-1
    ; 7. $h_pathNew = "" file will be overwritten
    ; $h_pathNew = ... the result is saved in new path
    ; 8. $i_inEx = 0 delete
    ; $i_inEx = 1 keep
    ;
    ; Requirement(s): #include <File.au3> and #include <Array.au3>
    ; Return Value(s): 1 success
    ; -1 file not found
    ; -2 invalid $opt
    ; -3 invalid path to write
    ; Extented = Number of lines matched
    ; Author(s): th.meger
    ;
    ;===============================================================================
    ;

    [/autoit] [autoit][/autoit] [autoit]

    Func _removeLineInFile($h_path, $s_search, $i_opt = 1, $i_caseSensitive = 0, $i_count = 0, $i_topDown = 0, $h_pathNew = "", $i_inEx = 0)
    Local $i_countFound = 0
    Local $a_FileOne
    Local $a_FileTwo[1]
    Local $a_FileThree[1]

    If $h_pathNew = "" Then $h_pathNew = $h_path
    If $i_count = 0 Then $i_count = -1

    If Not _FileReadToArray($h_path, $a_FileOne) Then Return -1

    _ArrayDelete($a_FileOne, 0) ; do not need the number

    If $i_topDown = 1 Then _ArrayReverse($a_FileOne)

    For $i = 0 To UBound($a_FileOne) - 1
    If $i_countFound <> $i_count Then
    Switch $i_opt
    Case 1
    If $i_caseSensitive = 1 Then
    If $a_FileOne[$i] == $s_search Then
    $i_countFound += 1
    _ArrayAdd($a_FileThree, $a_FileOne[$i])
    Else
    _ArrayAdd($a_FileTwo, $a_FileOne[$i])
    EndIf
    Else
    If $a_FileOne[$i] = $s_search Then
    $i_countFound += 1
    _ArrayAdd($a_FileThree, $a_FileOne[$i])
    Else
    _ArrayAdd($a_FileTwo, $a_FileOne[$i])
    EndIf
    EndIf
    Case 2
    If $i_caseSensitive = 1 Then
    If StringLeft($a_FileOne[$i], StringLen($s_search)) == $s_search Then
    $i_countFound += 1
    _ArrayAdd($a_FileThree, $a_FileOne[$i])
    Else
    _ArrayAdd($a_FileTwo, $a_FileOne[$i])
    EndIf
    Else
    If StringLeft($a_FileOne[$i], StringLen($s_search)) = $s_search Then
    $i_countFound += 1
    _ArrayAdd($a_FileThree, $a_FileOne[$i])
    Else
    _ArrayAdd($a_FileTwo, $a_FileOne[$i])
    EndIf
    EndIf
    Case 3
    If $i_caseSensitive = 1 Then
    If StringRight($a_FileOne[$i], StringLen($s_search)) == $s_search Then
    $i_countFound += 1
    _ArrayAdd($a_FileThree, $a_FileOne[$i])
    Else
    _ArrayAdd($a_FileTwo, $a_FileOne[$i])
    EndIf
    Else
    If StringRight($a_FileOne[$i], StringLen($s_search)) = $s_search Then
    $i_countFound += 1
    _ArrayAdd($a_FileThree, $a_FileOne[$i])
    Else
    _ArrayAdd($a_FileTwo, $a_FileOne[$i])
    EndIf
    EndIf
    Case 4
    If StringInStr($a_FileOne[$i], $s_search, $i_caseSensitive) = 0 Then
    _ArrayAdd($a_FileTwo, $a_FileOne[$i])
    Else
    $i_countFound += 1
    _ArrayAdd($a_FileThree, $a_FileOne[$i])
    EndIf
    Case Else
    Return -2
    EndSwitch
    Else
    If $i_inEx = 0 Then
    _ArrayAdd($a_FileTwo, $a_FileOne[$i])
    _ArrayAdd($a_FileThree, $a_FileOne[$i])
    EndIf
    EndIf
    Next
    _ArrayDelete($a_FileTwo, 0)
    _ArrayDelete($a_FileThree, 0)

    If $i_topDown = 1 Then
    _ArrayReverse($a_FileTwo)
    _ArrayReverse($a_FileThree)
    EndIf

    If $i_inEx = 0 Then
    _FileWriteFromArray($h_pathNew, $a_FileTwo)
    Else
    _FileWriteFromArray($h_pathNew, $a_FileThree)
    EndIf
    SetExtended($i_countFound)
    If @error = 0 Then Return 1
    Return -3
    EndFunc ;==>_removeLineInFile

    [/autoit]

    So long,

    @admis: konnte es nicht in Skripte packen, daher hier. Bei Gelegenheit bitte umhängen. Danke!

    Mega