Xenobiologist Posted October 19, 2006 Share Posted October 19, 2006 Hi, I tried to write an UDF for working in files. This func removes or keeps line(s) in a file. You can choose between : 1. The line matches a search pattern completely 2. The line starts with the search pattern 3. The line ends with the search pattern 3. The line contains the search pattern Save result in the given File or a new file. expandcollapse popup#include <File.au3> #include <Array.au3> Global $path = "test.txt" Global $NewPath = "test1.txt" Global $search = "Autoit" ;$h_path, $s_search, $i_opt = 1, $i_caseSensitive = 0, $i_count = 0, $i_topDown = 0, $h_pathNew = "", $i_inEx = 0 ; 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) 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/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 ; ;=============================================================================== ; 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 Tell what you think... So long, Mega Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted October 19, 2006 Moderators Share Posted October 19, 2006 (edited) Ha, and what was that you once put in one of my UDF's? Something like 44 lines for a function seemed like alot? (add all your _arrayadd()/_arraydelete()/_filereadtoarray()/_filewritefromarray()/etc... and you're at like 400 lines Edit:Had to find it for reference http://www.autoitscript.com/forum/index.ph...st&p=246442 Edited October 19, 2006 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Xenobiologist Posted October 19, 2006 Author Share Posted October 19, 2006 :"> :"> :"> :"> :"> :"> :"> :"> :"> :"> :"> :"> :"> :"> :"> :"> :"> :"> Hi, I wished I had never posted that. Now you got me. Wasn't meant to be criticism. It was more a joke. So long, Mega Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times Link to comment Share on other sites More sharing options...
Xenobiologist Posted October 21, 2006 Author Share Posted October 21, 2006 Nobody? Why? Crap? So long, Mega Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now