Ashalshaikh Posted September 13, 2010 Share Posted September 13, 2010 Hi ..First! I started learning English in the past few months ... If there are some mistakes , Excuse me for this ...File Line UDFIt's very useful UDF !!Functions :-; _StringInLine; _FileLineDownByText; _FileLineDownByNum; _FileLineUpByText; _FileLineUpByNum; _FileLineSwap; _FileLineSort; _FileLineIsEmpty; _FileLineStripWS; _FileLineExists; _FileLineGetLonger; _FileLineGetShorter; _FileLineMove; _FileLineDelete; _FileLineAddNumbers; _FileLineAddStart; _FileLineAddEnd; _FileLineDisplay; _FileLineGetRefined; _FileLineLen; _FileLineReplace; _FileLineMerge; _FileLineGetInfoexpandcollapse popup#include-once ;=== UDF Info ============================================== ; UDF Name : File Line UDF. ; UDF Description : To control and Get informations. ; Author : Ashalshaikh : Ahmad Alshaikh. ; الســــــعودية ; Functions : ; _StringInLine ; _FileLineDownByText ; _FileLineDownByNum ; _FileLineUpByText ; _FileLineUpByNum ; _FileLineSwap ; _FileLineSort ; _FileLineIsEmpty ; _FileLineStripWS ; _FileLineExists ; _FileLineGetLonger ; _FileLineGetShorter ; _FileLineMove ; _FileLineDelete ; _FileLineAddNumbers ; _FileLineAddStart ; _FileLineAddEnd ; _FileLineDisplay ; _FileLineGetRefined ; _FileLineLen ; _FileLineReplace ; _FileLineMerge ; _FileLineGetInfo ; _IsFileOpen ;functions not completed ; _FileLineCenter ;Special Thanks : ; Alzri2 ; AutoIt Forums ; Zyzoom.org/vb ; And You ^_^ ;=== UDF Info ================= End ======================== ;======== #Include ========= #include <File.au3> #include <Array.au3> ;======= Functions ========= ; #FUNCTION# ==================================================================================================================== ; Name...........: _StringInLine ; Description ...: Find Some String In File Lines And Return Lines Nuber In Array ; Syntax.........: _StringInLine (FilePath,String,[CompreType]) ; Parameters ....: FilePath - Path Of File. ; String - String ; CompreType - 0 => Find If String In Line (StringInStr) :: 1=> Find If String=Line ; Return values .: Success - Lines Numbers In Array ; Failure - First Row In Array = 0 ; @Error = ; 0 = NoError ; 1 = Error opening specified file ; Author ........: Ashalshaikh : Ahmad Alshaikh ; =============================================================================================================================== Func _StringInLine($FilePath, $String, $CompreType = 0) If Not _IsFileOpen($FilePath) Then SetError(1) Return 0 EndIf Local $ReSult = "", $swd = "" If $CompreType <> 0 And $CompreType <> 1 Then $CompreType = 0 $LineNum = _FileCountLines($FilePath) For $i = 1 To $LineNum Step 1 $Read = FileReadLine($FilePath, $i) If $CompreType = 1 Then If $Read = $String Then $ReSult &= $swd & $i $swd = "|" EndIf Else If StringInStr($Read, $String) Then $ReSult &= $swd & $i $swd = "|" EndIf EndIf Next If Not $Read = $String Then Return 0 If $ReSult = "" Then Return StringSplit($ReSult, "") Else Return StringSplit($ReSult, "|") EndIf EndFunc ;==>_StringInLine ; #FUNCTION# ==================================================================================================================== ; Name...........: _FileLineDownByText ; Description ...: Down Line In File ; Syntax.........: _FileLineDownByText (FilePath,FullTextOnLine,[Flag]) ; Parameters ....: FilePath - Path Of File. ; FullTextOnLine - Text In That Line ; Flag - 0 -> Down All Lines = FullTextOnLine ; 1,2,3,n-> First Line = FullTextOnLine , Second Line = FullTextOnLine , Nth Line = FullTextOnLine ; Return values .: Success - Return 1 ; Failure - Return 0 ; @Error = ; 0 = NoError ; 1 = Error opening specified file ; 2 = Flag Is Wrong ; Remarks .......: Must FullTextOnLine=Line completely ; Author ........: Ashalshaikh : Ahmad Alshaikh ; =============================================================================================================================== Func _FileLineDownByText($FilePath, $FullTextOnLine, $Flag = 0) If Not _IsFileOpen($FilePath) Then SetError(1) Return 0 EndIf If $Flag <> 0 And Not _FileLineExists($FilePath, $Flag) Then SetError(2) Return 0 EndIf $LineNum = _StringInLine($FilePath, $FullTextOnLine, 1) If $LineNum[0] = 0 Then Return 0 Else If $Flag = 0 Then Local $RES = "" For $x = 1 To $LineNum[0] $RES &= "|" & _FileLineSwap($FilePath, $LineNum[$x], $LineNum[$x] + 1) Next Return StringTrimLeft($RES, 1) Else Return _FileLineSwap($FilePath, $LineNum[$Flag], $LineNum[$Flag] + 1) EndIf EndIf EndFunc ;==>_FileLineDownByText ; #FUNCTION# ==================================================================================================================== ; Name...........: _FileLineUpByText ; Description ...: Up Line In File ; Syntax.........: _FileLineUpByText (FilePath,FullTextOnLine,[Flag]) ; Parameters ....: FilePath - Path Of File. ; FullTextOnLine - Text In That Line ; Flag - 0 -> Down All Lines = FullTextOnLine ; 1,2,3,n-> First Line = FullTextOnLine , Second Line = FullTextOnLine , Nth Line = FullTextOnLine ; Return values .: Success - Return 1 ; Failure - Return 0 ; @Error = ; 0 = NoError ; 1 = Error opening specified file ; 2 = Flag Is Wrong ; Remarks .......: Must FullTextOnLine=Line completely ; Author ........: Ashalshaikh : Ahmad Alshaikh ; =============================================================================================================================== Func _FileLineUpByText($FilePath, $FullTextOnLine, $Flag = 0) If Not _IsFileOpen($FilePath) Then SetError(1) Return 0 EndIf If $Flag <> 0 And Not _FileLineExists($FilePath, $Flag) Then SetError(2) Return 0 EndIf $LineNum = _StringInLine($FilePath, $FullTextOnLine, 1) If $LineNum[0] = 0 Then Return 0 Else If $Flag = 0 Then Local $RES = "" For $x = 1 To $LineNum[0] $RES &= "|" & _FileLineSwap($FilePath, $LineNum[$x], $LineNum[$x] - 1) Next Return StringTrimLeft($RES, 1) Else Return _FileLineSwap($FilePath, $LineNum[$Flag], $LineNum[$Flag] - 1) EndIf EndIf EndFunc ;==>_FileLineUpByText ; #FUNCTION# ==================================================================================================================== ; Name...........: _FileLineSwap ; Description ...: Swaps two Lines. ; Syntax.........: _FileLineSwap(FilePath, Line1Num, Line2Num) ; Parameters ....: FilePath - Path Of File. ; Line1Num - Number Of First Line ; Line2Num - Number Of Second Line ; Return values .: Success - 1 ; Failure - 0 ; @Error = ; 0 = NoError ; 1 = Error opening specified file ; Author ........: Ashalshaikh : Ahmad Alshaikh ; =============================================================================================================================== Func _FileLineSwap($FilePath, $Line1Num, $Line2Num) If Not _IsFileOpen($FilePath) Then SetError(1) Return 0 EndIf If Not _FileLineExists($FilePath, $Line1Num) Or Not _FileLineExists($FilePath, $Line2Num) Then Return 0 Local $Line1 = FileReadLine($FilePath, $Line1Num) If @error Then Return 0 Local $Line2 = FileReadLine($FilePath, $Line2Num) If @error Then Return 0 _FileWriteToLine($FilePath, $Line1Num, $Line2, 1) If @error Then Return 0 _FileWriteToLine($FilePath, $Line2Num, $Line1, 1) If @error Then Return 0 Return 1 EndFunc ;==>_FileLineSwap ; #FUNCTION# ==================================================================================================================== ; Name...........: _FileLineSort ; Description ...: Sort Lines ; Syntax.........: _FileLineSort (FilePath,[iDescending=0]) ; Parameters ....: FilePath - Path Of File. ; iDescending - [optional] If set to 1, sort descendingly ; Return values .: Success - 1 ; Failure - 0 ; @Error = ; 0 = NoError ; 1 = Error opening specified file ; Author ........: Ashalshaikh : Ahmad Alshaikh ; =============================================================================================================================== Func _FileLineSort($FilePath, $iDescending = 0) If Not _IsFileOpen($FilePath) Then SetError(1) Return 0 EndIf If $iDescending > 1 And $iDescending < 0 Then $iDescending = 0 Local $Arr _FileReadToArray($FilePath, $Arr) _ArrayDelete($Arr, 0) _ArraySort($Arr, $iDescending) Local $FO = FileOpen($FilePath, 2) If $FO = -1 Then SetError(1) Return 0 EndIf For $x = 0 To UBound($Arr) - 1 FileWriteLine($FO, $Arr[$x]) Next FileClose($FO) Return 1 EndFunc ;==>_FileLineSort ; #FUNCTION# ==================================================================================================================== ; Name...........: _FileLineIsEmpty ; Description ...: Check If Line IS Empty ; Syntax.........: _FileLineIsEmpty(FilePath, LineNum) ; Parameters ....: FilePath - Path Of File. ; LineNum - Number Of Line ; Return values .: Success - 1 ; Failure - 0 ; @Error = ; 0 = NoError ; 1 = Error opening specified file ; 2 = LineNum > _FileCountLines ; Author ........: Ashalshaikh : Ahmad Alshaikh ; =============================================================================================================================== Func _FileLineIsEmpty($FilePath, $LineNum) If Not _IsFileOpen($FilePath) Then SetError(1) Return 0 EndIf If Not _FileLineExists($FilePath, $LineNum) Then SetError(2) Return 0 EndIf If StringStripWS(FileReadLine($FilePath, $LineNum), 3) = "" Then Return True Return False EndFunc ;==>_FileLineIsEmpty ; #FUNCTION# ==================================================================================================================== ; Name...........: _FileLineStripWS ; Description ...: Strip Spaces In Lines ; Syntax.........: _FileLineStripWS(FilePath) ; Parameters ....: FilePath - Path Of File. ; Return values .: Success - Return 1 ; Failure - Return 0 ; @Error = ; 0 = NoError ; 1 = Error opening specified file ; Author ........: Ashalshaikh : Ahmad Alshaikh ; =============================================================================================================================== Func _FileLineStripWS($FilePath) If Not _IsFileOpen($FilePath) Then SetError(1) Return 0 EndIf Local $Arr _FileReadToArray($FilePath, $Arr) _ArrayDelete($Arr, 0) Local $FO = FileOpen($FilePath, 2) If $FO = -1 Then SetError(1) Return 0 EndIf For $x = 0 To UBound($Arr) - 1 If StringStripWS($Arr[$x], 3) = "" Then ContinueLoop FileWriteLine($FO, StringStripWS($Arr[$x], 3)) Next FileClose($FO) Return 1 EndFunc ;==>_FileLineStripWS ; #FUNCTION# ==================================================================================================================== ; Name...........: _FileLineExists ; Description ...: Check If Line Exists ; Syntax.........: _FileLineExists(FilePath,LineNum) ; Parameters ....: FilePath - Path Of File. ; LineNum - Number Of Line ; Return values .: Success - Return 1 ; Failure - Return 0 ; @Error = ; 0 = NoError ; 1 = Error opening specified file ; Author ........: Ashalshaikh : Ahmad Alshaikh ; =============================================================================================================================== Func _FileLineExists($FilePath, $LineNum) If Not _IsFileOpen($FilePath) Then SetError(1) Return 0 EndIf Local $CL = _FileCountLines($FilePath) If @error Then SetError(1) Return 0 EndIf If $LineNum > $CL Then Return False If $LineNum <= 0 Then Return False Return True EndFunc ;==>_FileLineExists ; #FUNCTION# ==================================================================================================================== ; Name...........: _FileLineCenter ; Description ...: Makes all the lines in the center ; Syntax.........: _FileLineCenter(FilePath, [Chr]) ; Parameters ....: FilePath - Path Of File. ; Chr - [optional] The reference to fill in the blanks [Default=" "]. ; Return values .: Success - 1 ; Failure - 0 ; @Error = ; 0 = NoError ; 1 = Error opening specified file ; Author ........: Ashalshaikh : Ahmad Alshaikh ; =============================================================================================================================== Func _FileLineCenter($FilePath, $Chr = " ") If Not _IsFileOpen($FilePath) Then SetError(1) Return 0 EndIf $Longer = _FileLineGetLonger($FilePath) Local $Arr _FileReadToArray($FilePath, $Arr) If @error Then SetError(@error) Return 0 EndIf _ArrayDelete($Arr, 0) Local $FO = FileOpen($FilePath, 2) If $FO = -1 Then SetError(1) Return 0 EndIf For $x = 0 To UBound($Arr) - 1 $diff = $Longer[1] - StringLen($Arr[$x]) For $e = 1 To $diff / 2 / StringLen($Chr) $Arr[$x] = $Chr & $Arr[$x] & $Chr Next FileWriteLine($FO, $Arr[$x]) Next FileClose($FO) Return 1 EndFunc ;==>_FileLineCenter ; #FUNCTION# ==================================================================================================================== ; Name...........: _FileLineGetLonger ; Description ...: Get Longer Line In File ; Syntax.........: _FileLineGetLonger(FilePath) ; Parameters ....: FilePath - Path Of File. ; Return values .: Success - Array (Row 0 : Line Numbeer , Row 1 : length Of Longer Line ) ; Failure - Arrau Row 0 : 0 ; @Error = ; 0 = NoError ; 1 = Error opening specified file ; 2 = Unable to Split the file ; Author ........: Ashalshaikh : Ahmad Alshaikh ; =============================================================================================================================== Func _FileLineGetLonger($FilePath) Local $Err[1] = [0] If Not _IsFileOpen($FilePath) Then SetError(1) Return $Err EndIf Local $Arr _FileReadToArray($FilePath, $Arr) If @error Then SetError(@error) Return $Err EndIf _ArrayDelete($Arr, 0) Local $Longer = 0, $Line = 0 For $x = 0 To UBound($Arr) - 1 If StringLen($Arr[$x]) > $Longer Then $Longer = StringLen($Arr[$x]) $Line = $x + 1 EndIf Next Local $RES[2] = [$Line, $Longer] Return $RES EndFunc ;==>_FileLineGetLonger ; #FUNCTION# ==================================================================================================================== ; Name...........: _FileLineGetShorter ; Description ...: Get Shorter Line In File ; Syntax.........: _FileLineGetShorter(FilePath) ; Parameters ....: FilePath - Path Of File. ; Return values .: Success - Array (Row 0 : Line Numbeer , Row 1 : length Of Shorter Line ) ; Failure - Arrau Row 0 : 0 ; @Error = ; 0 = NoError ; 1 = Error opening specified file ; 2 = Unable to Split the file ; Author ........: Ashalshaikh : Ahmad Alshaikh ; =============================================================================================================================== Func _FileLineGetShorter($FilePath) Local $Err[1] = [0] If Not _IsFileOpen($FilePath) Then SetError(1) Return $Err EndIf Local $Arr _FileReadToArray($FilePath, $Arr) If @error Then SetError(@error) Return $Err EndIf _ArrayDelete($Arr, 0) Local $REP = _FileLineGetLonger($FilePath) Local $Shorter = $REP[1], $Line = 0 For $x = 0 To UBound($Arr) - 1 If StringLen($Arr[$x]) < $Shorter Then $Shorter = StringLen($Arr[$x]) $Line = $x + 1 EndIf Next Local $RES[2] = [$Line, $Shorter] Return $RES EndFunc ;==>_FileLineGetShorter ; #FUNCTION# ==================================================================================================================== ; Name...........: _FileLineMove ; Description ...: Move Line ; Syntax.........: _FileLineMove(FilePath, From, TO) ; Parameters ....: FilePath - Path Of File. ; From - Number Of Line ; From - New Number Of Line ; Return values .: Success - 1 ; Failure - 0 ; @Error = ; 0 = NoError ; 1 = Error opening specified file ; 2 = Unable to Split the file ; Author ........: Ashalshaikh : Ahmad Alshaikh ; =============================================================================================================================== Func _FileLineMove($FilePath, $From, $TO) If Not _IsFileOpen($FilePath) Then SetError(1) Return 0 EndIf If Not _FileLineExists($FilePath, $From) Or Not _FileLineExists($FilePath, $TO) Then SetError(2) Return 0 EndIf Local $Arr _FileReadToArray($FilePath, $Arr) If @error Then SetError(@error) Return 0 EndIf If $From > $TO Then For $x = $From To $TO + 1 Step -1 _ArraySwap($Arr[$x], $Arr[$x - 1]) Next Else For $x = $From To $TO - 1 Step 1 _ArraySwap($Arr[$x], $Arr[$x + 1]) Next EndIf Local $FO = FileOpen($FilePath, 2) If $FO = -1 Then SetError(1) Return 0 EndIf For $x = 1 To UBound($Arr) - 1 FileWriteLine($FO, $Arr[$x]) Next FileClose($FO) Return 1 EndFunc ;==>_FileLineMove ; #FUNCTION# ==================================================================================================================== ; Name...........: _FileLineDisplay ; Description ...: Display File Lindes (Like : _ArrayDisplay) ; Syntax.........: _ArrayDisplay($FilePath [, $sTitle = "File Lines : ListView Display"[, $iItemLimit = -1[, $iTranspose = 0[, $sSeparator = ""[, $sReplace = "|"]]]]]) ; Parameters ....: FilePath - Path Of File. ; $sTitle - [optional] Title to use for window ; $iItemLimit - [optional] Maximum number of listview items (rows) to show ; $iTranspose - [optional] If set differently than default, will transpose the array if 2D ; $sSeparator - [optional] Change Opt("GUIDataSeparatorChar") on-the-fly ; $sReplace - [optional] String to replace any occurrence of $sSeparator with in each array element ; Return values .: Success - 1 ; Failure - 0 ; @Error = ; 0 = NoError ; 1 = Error opening specified file ; 2 = Unable to Split the file ; Author ........: Ashalshaikh : Ahmad Alshaikh ; =============================================================================================================================== Func _FileLineDisplay($FilePath, $sTitle = "File Lines : ListView Display", $iItemLimit = -1, $iTranspose = 0, $sSeparator = "", $sReplace = "|") Local $Arr _FileReadToArray($FilePath, $Arr) If @error Then SetError(@error) Return 0 EndIf Return _ArrayDisplay($Arr, $sTitle, $iItemLimit, $iTranspose, $sSeparator, $sReplace) EndFunc ;==>_FileLineDisplay ; #FUNCTION# ==================================================================================================================== ; Name...........: _FileLineDelete ; Description ...: Delete Line By Num. ; Syntax.........: _FileLineDelete(FilePath,LineNum) ; Parameters ....: FilePath - Path Of File. ; LineNum - Number Of Line. ; Return values .: Success - Return 1 ; Failure - Return 0 ; @Error = ; 0 = NoError ; 1 = Error opening specified file ; Author ........: Ashalshaikh : Ahmad Alshaikh ; =============================================================================================================================== Func _FileLineDelete($FilePath, $LineNum) If Not _IsFileOpen($FilePath) Then SetError(1) Return 0 EndIf Local $Arr _FileReadToArray($FilePath, $Arr) Local $FO = FileOpen($FilePath, 2) If $FO = -1 Then SetError(1) Return 0 EndIf For $x = 1 To UBound($Arr) - 1 If $x = $LineNum Then ContinueLoop FileWriteLine($FO, $Arr[$x]) Next FileClose($FO) Return 1 EndFunc ;==>_FileLineDelete ; #FUNCTION# ==================================================================================================================== ; Name...........: _FileLineAddNumbers ; Description ...: Add Numbers at Lines ; Syntax.........: _FileLineAddNumbers($FilePath, [$StartWork = 1, [$EndWork = -1, [$StartNum = 1, [$AfterNum = " "]]]]) ; Parameters ....: FilePath - Path Of File. ; StartWork - LineNum Of Start Add Numbers ; EndWork - LineNum Of End Add Numbers ; StartNum - Number which begins by numbering ; AfterNum - Any Thing Add After Num ::: For Example " - " Because Num To Be " 1 - Line " , " 222 - Line". ; Return values .: Success - Return 1 ; Failure - Return 0 ; @Error = ; 0 = NoError ; 1 = Error opening specified file ; Author ........: Ashalshaikh : Ahmad Alshaikh ; =============================================================================================================================== Func _FileLineAddNumbers($FilePath, $StartWork = 1, $EndWork = -1, $StartNum = 1, $AfterNum = " ") If Not _IsFileOpen($FilePath) Then SetError(1) Return 0 EndIf If $EndWork = -1 Then $EndWork = _FileCountLines($FilePath) Local $Arr _FileReadToArray($FilePath, $Arr) Local $FO = FileOpen($FilePath, 2) If $FO = -1 Then SetError(1) Return 0 EndIf For $x = 1 To UBound($Arr) - 1 If $x >= $StartWork Then If $x <= $EndWork Then $Arr[$x] = $StartNum & $AfterNum & $Arr[$x] $StartNum += 1 EndIf EndIf Next For $x = 1 To UBound($Arr) - 1 FileWriteLine($FO, $Arr[$x]) Next FileClose($FO) Return 1 EndFunc ;==>_FileLineAddNumbers ; #FUNCTION# ==================================================================================================================== ; Name...........: _FileLineUpByNum ; Description ...: Up Line By Its Number. ; Syntax.........: _FileLineUpByNum(FilePath, LineNum) ; Parameters ....: FilePath - Path Of File. ; LineNum - Number Of Line ; Return values .: Success - Return 1 ; Failure - Return 0 ; @Error = ; 0 = NoError ; 1 = Error opening specified file ; 2 = Line Not Found. ; Author ........: Ashalshaikh : Ahmad Alshaikh ; =============================================================================================================================== Func _FileLineUpByNum($FilePath, $LineNum) If Not _IsFileOpen($FilePath) Then SetError(1) Return 0 EndIf If Not _FileLineExists($FilePath, $LineNum) Then SetError(2) Return 0 EndIf Return _FileLineSwap($FilePath, $LineNum, $LineNum - 1) EndFunc ;==>_FileLineUpByNum ; #FUNCTION# ==================================================================================================================== ; Name...........: _FileLineDownByNum ; Description ...: Down Line By Its Number. ; Syntax.........: _FileLineDownByNum(FilePath, LineNum) ; Parameters ....: FilePath - Path Of File. ; LineNum - Number Of Line ; Return values .: Success - Return 1 ; Failure - Return 0 ; @Error = ; 0 = NoError ; 1 = Error opening specified file ; 2 = Line Not Found. ; Author ........: Ashalshaikh : Ahmad Alshaikh ; =============================================================================================================================== Func _FileLineDownByNum($FilePath, $LineNum) If Not _IsFileOpen($FilePath) Then SetError(1) Return 0 EndIf If Not _FileLineExists($FilePath, $LineNum) Then SetError(2) Return 0 EndIf Return _FileLineSwap($FilePath, $LineNum, $LineNum + 1) EndFunc ;==>_FileLineDownByNum ; #FUNCTION# ==================================================================================================================== ; Name...........: _FileLineAddStart ; Description ...: Add String For Start Of Lines. ; Syntax.........: _FileLineAddStart($FilePath, $String, [$StartWork = 1, [$EndWork = -1]]) ; Parameters ....: FilePath - Path Of File. ; $String - String Which Add it. ; $StartWork - [optional] Number Of Line Which Start Adding From It. :{Default = 1} ; $EndWork - [optional] Number Of Line Which End Adding From It. :{Default = -1 , to End File} ; Return values .: Success - Return 1 ; Failure - Return 0 ; @Error = ; 0 = NoError ; 1 = Error opening specified file ; Author ........: Ashalshaikh : Ahmad Alshaikh ; =============================================================================================================================== Func _FileLineAddStart($FilePath, $String, $StartWork = 1, $EndWork = -1) If Not _IsFileOpen($FilePath) Then SetError(1) Return 0 EndIf If $EndWork = -1 Then $EndWork = _FileCountLines($FilePath) Local $Arr _FileReadToArray($FilePath, $Arr) Local $FO = FileOpen($FilePath, 2) If $FO = -1 Then SetError(1) Return 0 EndIf For $x = 1 To UBound($Arr) - 1 If $x >= $StartWork Then If $x <= $EndWork Then $Arr[$x] = $String & $Arr[$x] EndIf EndIf Next For $x = 1 To UBound($Arr) - 1 FileWriteLine($FO, $Arr[$x]) Next FileClose($FO) Return 1 EndFunc ;==>_FileLineAddStart ; #FUNCTION# ==================================================================================================================== ; Name...........: _FileLineAddEnd ; Description ...: Add String For End Of Lines. ; Syntax.........: _FileLineAddEnd($FilePath, $String, [$StartWork = 1, [$EndWork = -1]]) ; Parameters ....: FilePath - Path Of File. ; $String - String Which Add it. ; $StartWork - [optional] Number Of Line Which Start Adding From It. :{Default = 1} ; $EndWork - [optional] Number Of Line Which End Adding From It. :{Default = -1 , to End File} ; Return values .: Success - Return 1 ; Failure - Return 0 ; @Error = ; 0 = NoError ; 1 = Error opening specified file ; Author ........: Ashalshaikh : Ahmad Alshaikh ; =============================================================================================================================== Func _FileLineAddEnd($FilePath, $String, $StartWork = 1, $EndWork = -1) If Not _IsFileOpen($FilePath) Then SetError(1) Return 0 EndIf If $EndWork = -1 Then $EndWork = _FileCountLines($FilePath) Local $Arr _FileReadToArray($FilePath, $Arr) Local $FO = FileOpen($FilePath, 2) If $FO = -1 Then SetError(1) Return 0 EndIf For $x = 1 To UBound($Arr) - 1 If $x >= $StartWork Then If $x <= $EndWork Then $Arr[$x] = $Arr[$x] & $String EndIf EndIf Next For $x = 1 To UBound($Arr) - 1 FileWriteLine($FO, $Arr[$x]) Next FileClose($FO) Return 1 EndFunc ;==>_FileLineAddEnd ; #FUNCTION# ==================================================================================================================== ; Name...........: _FileLineGetRefined ; Description ...: Get Refined Lines. ; Syntax.........: _FileLineAddEnd($FilePath) ; Parameters ....: FilePath - Path Of File. ; Return values .: Success - [Array] Refined Lines Number .. Like : 1+3+9 ; Failure - Return 0 ; @Error = ; 0 = NoError ; 1 = Error opening specified file ; Author ........: Ashalshaikh : Ahmad Alshaikh ; =============================================================================================================================== Func _FileLineGetRefined($FilePath) If Not _IsFileOpen($FilePath) Then SetError(1) Return 0 EndIf Local $Arr _FileReadToArray($FilePath, $Arr) Local $RES = "" For $e = 1 To UBound($Arr) - 1 $RES &= "#" & $e For $w = $e + 1 To UBound($Arr) - 1 If $Arr[$e] = $Arr[$w] Then $RES &= "+" & $w Next Next $RES = StringSplit(StringTrimLeft($RES, 1), "#") $r = 1 While UBound($RES) - 1 >= $r If Not StringInStr($RES[$r], "+") Then _ArrayDelete($RES, $r) $r -= 1 EndIf $r += 1 WEnd $RES[0] = UBound($RES) - 1 Return $RES EndFunc ;==>_FileLineGetRefined ; #FUNCTION# ==================================================================================================================== ; Name...........: _FileLineLen ; Description ...: Returns the number of characters in a Line ; Syntax.........: _FileLineLen($FilePath, $LineNum) ; Parameters ....: FilePath - Path Of File. ; LineNum - Number Of Line. ; Return values .: Success - the length of the Line. ; Failure - Return 0 ; @Error = ; 0 = NoError ; 1 = Error opening specified file ; Author ........: Ashalshaikh : Ahmad Alshaikh ; =============================================================================================================================== Func _FileLineLen($FilePath, $LineNum) If Not _IsFileOpen($FilePath) Then SetError(1) Return 0 EndIf If Not _FileLineExists($FilePath, $LineNum) Then SetError(2) Return 0 EndIf Local $Arr _FileReadToArray($FilePath, $Arr) Return StringLen($Arr[$LineNum]) EndFunc ;==>_FileLineLen ; #FUNCTION# ==================================================================================================================== ; Name...........: _FileLineReplace ; Description ...: Replace Line By New Text. ; Syntax.........: _FileLineReplace($FilePath, $LineNum, $NewText) ; Parameters ....: FilePath - Path Of File. ; LineNum - Number Of Line. ; NewText - New Text For Replace Line. ; Return values .: Success - Return 1 ; Failure - Return 0 ; @Error = ; 0 = NoError ; 1 = Error opening specified file ; Author ........: Ashalshaikh : Ahmad Alshaikh ; =============================================================================================================================== Func _FileLineReplace($FilePath, $LineNum, $NewText) If Not _IsFileOpen($FilePath) Then SetError(1) Return 0 EndIf If Not _FileLineExists($FilePath, $LineNum) Then SetError(2) Return 0 EndIf Return _FileWriteToLine($FilePath, $LineNum, $NewText, 1) EndFunc ;==>_FileLineReplace ; #FUNCTION# ==================================================================================================================== ; Name...........: _FileLineMerge ; Description ...: Merge Tow Files In 1st File. ; Syntax.........: _FileLineMerge($FilePath_1, $FilePath_2) ; Parameters ....: FilePath_1 - Path Of 1st File. ; FilePath_1 - Path Of 2ed File. ; Return values .: Success - Return 1 ; Failure - Return 0 ; @Error = ; 0 = NoError ; 1 = Error opening specified file ; Author ........: Ashalshaikh : Ahmad Alshaikh ; =============================================================================================================================== Func _FileLineMerge($FilePath_1, $FilePath_2) If Not _IsFileOpen($FilePath_1) Then SetError(1) Return 0 EndIf If Not _IsFileOpen($FilePath_2) Then SetError(2) Return 0 EndIf $FO1 = FileOpen($FilePath_1, 1) If $FO1 = -1 Then SetError(1) Return 0 EndIf $FO2 = FileOpen($FilePath_2, 0) If $FO2 = -1 Then SetError(2) Return 0 EndIf Local $S = FileWriteLine($FO1, FileRead($FO2)) FileClose($FO1) FileClose($FO2) Return $S EndFunc ;==>_FileLineMerge ; #FUNCTION# ==================================================================================================================== ; Name...........: FileLineGetInfo ; Description ...: Get Info About File Lines. ; Syntax.........: _FileLineGetInfo($FilePath) ; Parameters ....: FilePath - Path Of File. ; Return values .: Success - [Array] An 6 Column : ; 0 Col = Line Number ; 1 Col = Line Text ; 2 Col = Line Len ; 4 Col = Line Upped ; 5 Col = Line Lowed ; 5 Col = Check Line Is Empty ; Failure - [Array] = 0Row = 0 ; @Error = ; 0 = NoError ; 1 = Error opening specified file ; Author ........: Ashalshaikh : Ahmad Alshaikh ; =============================================================================================================================== Func _FileLineGetInfo($FilePath) Local $Err[1] = [0] If Not _IsFileOpen($FilePath) Then SetError(1) Return $Err EndIf Local $RES[_FileCountLines($FilePath) + 1][6] $RES[0][0] = "Line Num." $RES[0][1] = "Line Text" $RES[0][2] = "Line Len" $RES[0][3] = "Line Up" $RES[0][4] = "Line Low" $RES[0][5] = "Line IsEmpty" Local $Arr _FileReadToArray($FilePath, $Arr) For $x = 1 To $Arr[0] $RES[$x][0] = $x $RES[$x][1] = $Arr[$x] $RES[$x][2] = StringLen($Arr[$x]) $RES[$x][3] = StringUpper($Arr[$x]) $RES[$x][4] = StringLower($Arr[$x]) $RES[$x][5] = _FileLineIsEmpty($FilePath, $x) Next Return $RES EndFunc ;==>_FileLineGetInfo Func _IsFileOpen($FilePath) Local $D = FileOpen($FilePath, 0) FileClose($D) If $D = -1 Then Return 0 EndIf Return 1 EndFunc ;==>_IsFileOpenExamplesFile Line UDF Examples.au3Thanks .. Link to comment Share on other sites More sharing options...
wakillon Posted September 13, 2010 Share Posted September 13, 2010 Hi Ashalshaikh !Big udf, and can be usefullThanks to share AutoIt 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts 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