- Offizieller Beitrag
Hi,
ich habe mal mein Glück versucht und eine UDF geschrieben, die das Löschen in Dateien vereinfachen soll.
Vielleicht kann es ja jeamand gebrauchen. Ist der erste Wurf, also bitte nicht allzu hart mit der Kritik umsichwerfen. :kiss:
Spoiler anzeigen
PHP
#include <File.au3>
#include <Array.au3>
Global $path = "c:\Autoit_UDF.txt"
Global $NewPath = "c:\Autoit_UDF_removedLines.txt"
Global $search = "Autoit"
; 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, 3, 0, 0, 0, "")
; deletes the first line containing the string "Autoit" searching bottomUp
_removeLineInFile($path, $search, 3, 0, 1, 1, "")
; deletes the first 5 lines starting with "Autoit" case sensitive, searching topDown and save the result at $newPath
_removeLineInFile($path, $search, 3, 1, 5, 0, $NewPath)
MsgBox(64, "To see the return value", _removeLineInFile($path, $search, 3, 0, 0, 1, "")) ; display the return value
;===============================================================================
;
; Function Name: _removeLineInFile
; Description:: _removeLineInFile
; Parameter(s):
; 1. $h_path = Path to file
; 2. $s_search = string pattern
; 3. Opt = 1 Delete line if matches $s_search
; Opt = 2 Delete line if starts with $s_search
; Opt = 3 Delete line if contains $s_search
; Opt = 3 + Parameter 4 :
; 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
;
; 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
; Author(s): th.meger
;
;===============================================================================
;
Func _removeLineInFile($h_path, $s_search, $i_opt = 1, $i_CaseSensitive = 0, $i_count = 0, $i_TopDown = 0, $h_pathNew = "")
Local $i_countFound = 0
Local $a_FileOne
Local $a_FileTwo[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
Else
_ArrayAdd($a_FileTwo, $a_FileOne[$i])
EndIf
Else
If $a_FileOne[$i] = $s_search Then
$i_countFound += 1
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
Else
_ArrayAdd($a_FileTwo, $a_FileOne[$i])
EndIf
Else
If StringLeft($a_FileOne[$i], StringLen($s_search)) = $s_search Then
$i_countFound += 1
Else
_ArrayAdd($a_FileTwo, $a_FileOne[$i])
EndIf
EndIf
Case 3
If StringInStr($a_FileOne[$i], $s_search, $i_CaseSensitive) = 0 Then
_ArrayAdd($a_FileTwo, $a_FileOne[$i])
Else
$i_countFound += 1
EndIf
Case Else
Return -2
EndSwitch
Else
_ArrayAdd($a_FileTwo, $a_FileOne[$i])
EndIf
Next
_ArrayDelete($a_FileTwo, 0)
If $i_TopDown = 1 Then _ArrayReverse($a_FileTwo)
_FileWriteFromArray($h_pathNew, $a_FileTwo)
If @error = 0 Then Return 1
Return -3
EndFunc ;==>_removeLineInFile
Alles anzeigen
So long,
Mega