Jump to content

Solved:Tidy text file


Recommended Posts

Hi brothers,

I'm trying to tidy this file :
 

02-Jan-18 Is :
3
01-Jan-18 Is :
2
03-Jan-18 Is :
1

to make it like this :

01-Jan-18 Is :
2
02-Jan-18 Is :
3
03-Jan-18 Is :
1

I succeed to get list of dates by this code:
 

#include<array.au3>
;#include '_Startup.au3'
#include <Date.au3>
#include<File.au3>


$fileToRead = @ScriptDir & "\Test.txt"
    

;###################################### Load Days
        Local $FileRead = FileRead($fileToRead) & @CRLF
$aTxt = StringRegExpReplace($FileRead & @CRLF, "(.*?)(\r\n)(.*?)(\r\n)", "$1,");Read first line to array
$aTxt = StringSplit($aTxt, ",")
dim $Array_TotalHours[1]
dim $Array_ToFill[1]
For $i = 1 To UBound($aTxt) 
$Split_File=StringSplit($aTxt[$i],"-Jan");Split Month
;_ArrayDisplay($Split_File) 
If UBound($Split_File,1)=2 Then ; If not enough array rows Exitloop
    ExitLoop
EndIf
    ConsoleWrite(@CRLF&$Split_File[1]);Day raw
_ArrayAdd($Array_TotalHours, $Split_File[1])
_ArrayAdd($Array_ToFill, $Split_File[1])
Next
_ArrayDisplay($Array_ToFill)  ;Resault

but I can't figure how to make the smaller date comes first 

 

solved by:

@Earthshine

 

Edited by abdulrahmanok
Link to comment
Share on other sites

1 hour ago, Earthshine said:

 

firstly,thanks for this post it's helped me a lot
After using _DateDiff and StringReplace to convert date type this is my try:

Local $FileRead = FileRead($fileToRead) & @CRLF
$aTxt = StringRegExpReplace($FileRead & @CRLF, "(.*?)(\r\n)(.*?)(\r\n)", "$1,");Read first line to array
$aTxt = StringSplit($aTxt, ",")
;_ArrayDisplay($aTxt) 
;$sDate = $aTxt[1]
;$aDate = StringSplit($sDate,"-")

Dim $Arraytofill[1]
For $i = 1 to UBound($aTxt)

$aDate = StringSplit($aTxt[$i],"-")
If UBound($aDate,1)=2 Then ; If not enough array rows Exitloop
    ExitLoop
EndIf
$Convert =  StringReplace($aDate[3],$aDate[3],@YEAR) & "/" & StringReplace($aDate[2],$aDate[2],@MON) & "/" &$aDate[1]
$old=$aTxt[$i]
_ArrayAdd($Arraytofill,$Convert)
;MsgBox(0,"Old: "&$aTxt[$i],  $Convert)
Next
_ArrayDisplay($Arraytofill)

$iDateCalc = _DateDiff( 'D',$Arraytofill[2],$Arraytofill[1])
MsgBox( 4096, "", "Resault " & $iDateCalc )
if $iDateCalc >=1 Then
    MsgBox(0,"","This line must go down")
    Else
    MsgBox(0,"","Keep line")    
EndIf

but still need :

1- some way to compare between all array values .

2- I don't have idea about how to make this founded line go down :( 

Link to comment
Share on other sites

Whole code:
 

#include<array.au3>
;#include '_Startup.au3'
#include <Date.au3>
#include<File.au3>

$fileToRead = @ScriptDir & "\Test.txt"
    

;###################################### Load Days
        Local $FileRead = FileRead($fileToRead) & @CRLF
$aTxt = StringRegExpReplace($FileRead & @CRLF, "(.*?)(\r\n)(.*?)(\r\n)", "$1,");Read first line to array
$aTxt = StringSplit($aTxt, ",")
;_ArrayDisplay($aTxt) 
;$sDate = $aTxt[1]
;$aDate = StringSplit($sDate,"-")

Dim $Arraytofill[1]
For $i = 1 to UBound($aTxt)

$aDate = StringSplit($aTxt[$i],"-")
If UBound($aDate,1)=2 Then ; If not enough array rows Exitloop
    ExitLoop
EndIf
$Convert =  StringReplace($aDate[3],$aDate[3],@YEAR) & "/" & StringReplace($aDate[2],$aDate[2],@MON) & "/" &$aDate[1]
$old=$aTxt[$i]
_ArrayAdd($Arraytofill,$Convert)
;MsgBox(0,"Old: "&$aTxt[$i],  $Convert)
Next
_ArrayDisplay($Arraytofill)

$iDateCalc = _DateDiff( 'D',$Arraytofill[2],$Arraytofill[1])
MsgBox( 4096, "", "Resault " & $iDateCalc )
if $iDateCalc >=1 Then
    MsgBox(0,"","This line must go down")
    Else
    MsgBox(0,"","Keep line")    
EndIf

"Test.txt" contains :
 

02-Jan-18 Is :
3
01-Jan-18 Is :
2
03-Jan-18 Is :
1
Link to comment
Share on other sites

try this. added an _ArraySort with default paramters. Read the help file regarding For Loops for how to access all the elements of your array later in your program once built.

#include<array.au3>
;#include '_Startup.au3'
#include <Date.au3>
#include<File.au3>

$fileToRead = @ScriptDir & "\Test.txt"


;###################################### Load Days
        Local $FileRead = FileRead($fileToRead) & @CRLF
$aTxt = StringRegExpReplace($FileRead & @CRLF, "(.*?)(\r\n)(.*?)(\r\n)", "$1,");Read first line to array
$aTxt = StringSplit($aTxt, ",")
;_ArrayDisplay($aTxt)
;$sDate = $aTxt[1]
;$aDate = StringSplit($sDate,"-")

Dim $Arraytofill[1]
For $i = 1 to UBound($aTxt)

$aDate = StringSplit($aTxt[$i],"-")
If UBound($aDate,1)=2 Then ; If not enough array rows Exitloop
    ExitLoop
EndIf
$Convert =  StringReplace($aDate[3],$aDate[3],@YEAR) & "/" & StringReplace($aDate[2],$aDate[2],@MON) & "/" &$aDate[1]
$old=$aTxt[$i]
_ArrayAdd($Arraytofill,$Convert)
;MsgBox(0,"Old: "&$aTxt[$i],  $Convert)
Next
_ArraySort($Arraytofill)
_ArrayDisplay($Arraytofill)

$iDateCalc = _DateDiff( 'D',$Arraytofill[2],$Arraytofill[1])
MsgBox( 4096, "", "Resault " & $iDateCalc )
if $iDateCalc >=1 Then
    MsgBox(0,"","This line must go down")
    Else
    MsgBox(0,"","Keep line")
EndIf

 

Capture.PNG

Capture2.PNG

Capture3.PNG

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

to make it more clear I edited "Test.txt" to be :
 

02-Jan-18 Is :
Value for Jan2
01-Jan-18 Is :
Value for Jan1
03-Jan-18 Is :
Value for Jan3

 

and my code :

#include<array.au3>
;#include '_Startup.au3'
#include <Date.au3>
#include<File.au3>

$fileToRead = @ScriptDir & "\Test.txt"


;###################################### Load Days
        Local $FileRead = FileRead($fileToRead) & @CRLF
$aTxt = StringRegExpReplace($FileRead & @CRLF, "(.*?)(\r\n)(.*?)(\r\n)", "$1,");Read first line to array
$aTxt = StringSplit($aTxt, ",")
;_ArrayDisplay($aTxt)
;$sDate = $aTxt[1]
;$aDate = StringSplit($sDate,"-")


Dim $Arraytofill[1]
For $i = 1 to UBound($aTxt)

$aDate = StringSplit($aTxt[$i],"-")
If UBound($aDate,1)=2 Then ; If not enough array rows Exitloop
    ExitLoop
EndIf
$Convert =  StringReplace($aDate[3],$aDate[3],@YEAR) & "/" & StringReplace($aDate[2],$aDate[2],@MON) & "/" &$aDate[1]
$old=$aTxt[$i]
_ArrayAdd($Arraytofill,$Convert)
;MsgBox(0,"Old: "&$aTxt[$i],  $Convert)
Next
        
_ArraySort($Arraytofill)
_ArrayDelete($Arraytofill, 0) ;Delete Total Rows Value
_ArrayDisplay($Arraytofill)

$fileToRead = @ScriptDir & "\Test.txt"
        Local $FileRead = FileRead($fileToRead) & @CRLF
        $aTxt2 = StringRegExpReplace($FileRead & @CRLF, "(.*?)(\r\n)(.*?)(\r\n)", "$3,");Ready line and pass line
                $aTxt2 = StringSplit($aTxt2, ",");Ready line and pass line
        _ArrayDelete($aTxt2, 0) ;Delete Total Rows Value
_ArrayDisplay($aTxt2)

For $i = 0 to UBound($Arraytofill)-1
    FileWrite($fileToRead,@crlf&$Arraytofill[$i])
    FileWrite($fileToRead,@crlf&$aTxt2[$i])
Next

Result is :

02-Jan-18 Is :
Value for Jan2
01-Jan-18 Is :
Value for Jan1
03-Jan-18 Is :
Value for Jan3

2018/01/01
Value for Jan2
2018/01/02
Value for Jan1
2018/01/03
Value for Jan3

notice that second line is not true it must be like this :
2018/01/01

Value for Jan1
2018/01/02
Value for Jan2
2018/01/03
Value for Jan3

 

Link to comment
Share on other sites

Another try with _LineNumsOfSearchStr :D

#include<array.au3>
;#include '_Startup.au3'
#include <Date.au3>
#include<File.au3>
dim $foundedNUM
$fileToRead = @ScriptDir & "\Test.txt"


;###################################### Load Days
        Local $FileRead = FileRead($fileToRead) & @CRLF
$aTxt = StringRegExpReplace($FileRead & @CRLF, "(.*?)(\r\n)(.*?)(\r\n)", "$1,");Read first line to array
$aTxt = StringSplit($aTxt, ",")
_ArrayDelete($aTxt, 0) ;Delete Total Rows Value

;$sDate = $aTxt[1]
;$aDate = StringSplit($sDate,"-")


Dim $Arraytofill[1]
For $i = 0 to UBound($aTxt)

$aDate = StringSplit($aTxt[$i],"-")
If UBound($aDate,1)=2 Then ; If not enough array rows Exitloop
    ExitLoop
EndIf
$Convert =  StringReplace($aDate[3],$aDate[3],@YEAR) & "/" & StringReplace($aDate[2],$aDate[2],@MON) & "/" &$aDate[1]
$old=$aTxt[$i]
_ArrayAdd($Arraytofill,$Convert)
;MsgBox(0,"Old: "&$aTxt[$i],  $Convert)
Next
        
_ArraySort($Arraytofill)
_ArrayDelete($Arraytofill, 0) ;Delete Total Rows Value


$fileToRead = @ScriptDir & "\Test.txt"
        Local $FileRead = FileRead($fileToRead) & @CRLF
        $aTxt2 = StringRegExpReplace($FileRead & @CRLF, "(.*?)(\r\n)(.*?)(\r\n)", "$3,");Ready line and pass line
                $aTxt2 = StringSplit($aTxt2, ",");Ready line and pass line
        _ArrayDelete($aTxt2, 0) ;Delete Total Rows Value
;_ArrayDisplay($Arraytofill)
;_ArrayDisplay($aTxt2)


Dim $final_array[1]
For $i = 0 to UBound($aTxt,1)/2-1
;ConsoleWrite(@CRLF&$aTxt[$i])
$getLine=_LineNumsOfSearchStr($fileToRead,$aTxt[$i])
$Second_Line=FileReadLine($fileToRead,$foundedNUM+1)
;ConsoleWrite(@CRLF&"Next Line is: "&$Second_Line)
_ArrayAdd($final_array,$Second_Line)
Next
_ArrayDelete($final_array, 0) ;
_ArrayDisplay($Arraytofill)
_ArrayDisplay($final_array)


;~ $iDateCalc = _DateDiff( 'D',$Arraytofill[2],$Arraytofill[1])
;~ MsgBox( 4096, "", "Resault " & $iDateCalc )
;~ if $iDateCalc >=1 Then
;~     MsgBox(0,"","This line must go down")
;~     Else
;~     MsgBox(0,"","Keep line")
;~ EndIf
For $i = 0 to UBound($Arraytofill)-1
    ConsoleWrite(@CRLF&$Arraytofill[$i])
    ConsoleWrite(@CRLF&$final_array[$i])
    ;FileWrite($fileToRead,@crlf&$Arraytofill[$i])
    ;FileWrite($fileToRead,@crlf&$final_array[$i])
Next

Func _LineNumsOfSearchStr($sFileName, $searchString, $bDeleteLine = False)

    Local $location, $aCurrentLineNum, $iCurrentLineNum, $sFile, $iOccur = 1, $sRes = ""
    If FileExists($sFileName) = 0 Then Return 1
    Do
        $sFile = FileRead($sFileName)
        $location = StringInStr($sFile, $searchString, 0, $iOccur) ; Find the $iOccur occurrence of the "substring"

        If $location > 0 Then
            $aCurrentLineNum = StringRegExp(StringRegExpReplace($sFile, "(?s)(.{" & $location & "})(.*)$", "\1"), "(?s)(\v+)", 3) ;Find line number
            $iCurrentLineNum = UBound($aCurrentLineNum) + 1 ; Line number
            $foundedNUM=$iCurrentLineNum
;Consolewrite(@crlf&"Founded Date Line Is: "&$foundedNUM&@crlf)
        ;   _FileWriteToLine($sIniFile, 1, $iCurrentLineNum, True)
            ;ConsoleWrite("CharPos: " &  $location & "   Ln: " & $iCurrentLineNum & @CRLF)
            $sRes &= $iCurrentLineNum & "|"
            If $bDeleteLine Then
            Else
                $iOccur += 1
            EndIf
        Else
            ExitLoop
        EndIf
        Sleep(10)
    Until 0
    FileClose($sFile)
    ;ShellExecute($sFileName)
    Return StringSplit(StringTrimRight($sRes, 1), "|")
EndFunc   ;==>_LineNumsOfSearchStr

 

Link to comment
Share on other sites

Ty very much @Earthshine after playing around Finally  I did it :
 

#include<array.au3>
;#include '_Startup.au3'
#include <Date.au3>
#include<File.au3>
dim $foundedNUM

$fileToRead = @ScriptDir & "\Test.txt"
;###################################### Load Days
        Local $FileRead = FileRead($fileToRead)
$aTxt = StringRegExpReplace($FileRead & @CRLF, "(.*?)(\r\n)(.*?)(\r\n)", "$1,");Read first line to array
$aTxt = StringSplit($aTxt, ",")
_ArrayDelete($aTxt, 0) ;Delete Total Rows Value
Dim $Arraytofill[1]
For $i = 0 to UBound($aTxt,1) -1

;$aDate = StringSplit($aTxt[$i],"-")
;~ If UBound($aDate,1)=2 Then ; If not enough array rows Exitloop
;~     ExitLoop
;~ EndIf
;$Convert =  StringReplace($aDate[3],$aDate[3],@YEAR) & "/" & StringReplace($aDate[2],$aDate[2],@MON) & "/" &$aDate[1]
;$old=$aTxt[$i]
_ArrayAdd($Arraytofill,$aTxt[$i])
;MsgBox(0,"Old: "&$aTxt[$i],  $Convert)
Next    
_ArrayDelete($Arraytofill, 0) ;Delete Total Rows Value
_ArraySort($Arraytofill)
_ArrayDisplay($Arraytofill,"$Arraytofill")

$aTxt2 = StringRegExpReplace($FileRead & @CRLF, "(.*?)(\r\n)(.*?)(\r\n)", "$3,");Ready line and pass line
$aTxt2 = StringSplit($aTxt2, ",");Ready line and pass line
_ArrayDelete($aTxt2, 0) ;Delete Total Rows Value
Dim $final_array[1]
For $i = 0 to UBound($Arraytofill,1)-1
;ConsoleWrite(@CRLF&$aTxt[$i])
$getLine=_LineNumsOfSearchStr($fileToRead,$Arraytofill[$i])
$Second_Line=FileReadLine($fileToRead,$foundedNUM+1)
;ConsoleWrite(@CRLF&"Next Line is: "&$Second_Line)
_ArrayAdd($final_array,$Second_Line)
Next
_ArrayDelete($final_array, 0) ;
_ArrayDisplay($final_array)

For $i = 0 to UBound($Arraytofill,1)-1
    ConsoleWrite(@CRLF&$Arraytofill[$i])
    ConsoleWrite(@CRLF&$final_array[$i])
    ;FileWrite($fileToRead,@crlf&$Arraytofill[$i])
    ;FileWrite($fileToRead,@crlf&$final_array[$i])
Next

Func _LineNumsOfSearchStr($sFileName, $searchString, $bDeleteLine = False)

    Local $location, $aCurrentLineNum, $iCurrentLineNum, $sFile, $iOccur = 1, $sRes = ""
    If FileExists($sFileName) = 0 Then Return 1
    Do
        $sFile = FileRead($sFileName)
        $location = StringInStr($sFile, $searchString, 0, $iOccur) ; Find the $iOccur occurrence of the "substring"

        If $location > 0 Then
            $aCurrentLineNum = StringRegExp(StringRegExpReplace($sFile, "(?s)(.{" & $location & "})(.*)$", "\1"), "(?s)(\v+)", 3) ;Find line number
            $iCurrentLineNum = UBound($aCurrentLineNum) + 1 ; Line number
            $foundedNUM=$iCurrentLineNum
;Consolewrite(@crlf&"Founded Date Line Is: "&$foundedNUM&@crlf)
        ;   _FileWriteToLine($sIniFile, 1, $iCurrentLineNum, True)
            ;ConsoleWrite("CharPos: " &  $location & "   Ln: " & $iCurrentLineNum & @CRLF)
            $sRes &= $iCurrentLineNum & "|"
            If $bDeleteLine Then
            Else
                $iOccur += 1
            EndIf
        Else
            ExitLoop
        EndIf
        Sleep(10)
    Until 0
    FileClose($sFile)
    ;ShellExecute($sFileName)
    Return StringSplit(StringTrimRight($sRes, 1), "|")
EndFunc   ;==>_LineNumsOfSearchStr

Result:
 

01-Jan-18 Is :
Value for Jan1
02-Jan-18 Is :
Value for Jan2
03-Jan-18 Is :
Value for Jan3

 

Link to comment
Share on other sites

There is a little issue , when I put code between "Func" doesn't work correctly : 
 

_Sort_File()
Func _Sort_File()
dim $foundedNUM
$fileToRead = @ScriptDir & "\Test.txt"
;###################################### Load Days
        Local $FileRead = FileRead($fileToRead)
$aTxt = StringRegExpReplace($FileRead & @CRLF, "(.*?)(\r\n)(.*?)(\r\n)", "$1,");Read first line to array
$aTxt = StringSplit($aTxt, ",")
_ArrayDelete($aTxt, 0) ;Delete Total Rows Value
Dim $Arraytofill[1]
For $i = 0 to UBound($aTxt,1) -1
_ArrayAdd($Arraytofill,$aTxt[$i])
Next    
_ArrayDelete($Arraytofill, 0) ;Delete Total Rows Value
_ArraySort($Arraytofill)
_ArrayDisplay($Arraytofill,"$Arraytofill")

$aTxt2 = StringRegExpReplace($FileRead & @CRLF, "(.*?)(\r\n)(.*?)(\r\n)", "$3,");Ready line and pass line
$aTxt2 = StringSplit($aTxt2, ",");Ready line and pass line
_ArrayDelete($aTxt2, 0) ;Delete Total Rows Value
Dim $final_array[1]
For $i = 0 to UBound($Arraytofill,1)-1
    $fileToRead = @ScriptDir & "\Test.txt"
ConsoleWrite(@CRLF&$aTxt[$i])
$getLine=_LineNumsOfSearchStr($fileToRead,$Arraytofill[$i])
$Second_Line=FileReadLine($fileToRead,$foundedNUM+1)

;ConsoleWrite(@CRLF&"Next Line is: "&$Second_Line)
_ArrayAdd($final_array,$Second_Line)
Next
_ArrayDelete($final_array, 0) ;
_ArrayDisplay($final_array,"$final_array")
For $i = 0 to UBound($Arraytofill,1)-1
    ConsoleWrite(@CRLF&$Arraytofill[$i])
    ConsoleWrite(@CRLF&$final_array[$i])
    ;FileWrite($fileToRead,@crlf&$Arraytofill[$i])
    ;FileWrite($fileToRead,@crlf&$final_array[$i])
Next
EndFunc
Func _LineNumsOfSearchStr($sFileName, $searchString, $bDeleteLine = False)

    Local $location, $aCurrentLineNum, $iCurrentLineNum, $sFile, $iOccur = 1, $sRes = ""
    If FileExists($sFileName) = 0 Then Return 1
    Do
        $sFile = FileRead($sFileName)
         $location = StringInStr($sFile, $searchString, 0, $iOccur) ; Find the $iOccur occurrence of the "substring"

        If $location > 0 Then
             $aCurrentLineNum = StringRegExp(StringRegExpReplace($sFile, "(?s)(.{" & $location & "})(.*)$", "\1"), "(?s)(\v+)", 3) ;Find line number
             $iCurrentLineNum = UBound($aCurrentLineNum) + 1 ; Line number
            $foundedNUM=$iCurrentLineNum
;Consolewrite(@crlf&"Founded Date Line Is: "&$foundedNUM&@crlf)
        ;   _FileWriteToLine($sIniFile, 1, $iCurrentLineNum, True)
            ;ConsoleWrite("CharPos: " &  $location & "   Ln: " & $iCurrentLineNum & @CRLF)
            $sRes &= $iCurrentLineNum & "|"
            If $bDeleteLine Then
            Else
                $iOccur += 1
            EndIf
        Else
            ExitLoop
        EndIf
        Sleep(10)
    Until 0
    FileClose($sFile)
    ;ShellExecute($sFileName)
    Return StringSplit(StringTrimRight($sRes, 1), "|")
EndFunc   ;==>_LineNumsOfSearchStr

after remove (Func _Sort_File) every thing back to normal !

Edited by abdulrahmanok
Link to comment
Share on other sites

20 hours ago, abdulrahmanok said:

There is a little issue , when I put code between "Func" doesn't work correctly : 
 

 

Finally I solved this also by add :    

Return $foundedNUM

and make "$foundedNUM" Global 

Global  $foundedNUM=$iCurrentLineNum

Final code :

#include<array.au3>
;#include '_Startup.au3'
#include <Date.au3>
#include<File.au3>
 

Sort_File()
Func Sort_File()
    $fileToRead = @ScriptDir & "\daysarray.qu"

;dim $foundedNUM
;###################################### Load Days
        Local $FileRead = FileRead($fileToRead)
$aTxt = StringRegExpReplace($FileRead & @CRLF, "(.*?)(\r\n)(.*?)(\r\n)", "$1,");Read first line to array
$aTxt = StringSplit($aTxt, ",")
_ArrayDelete($aTxt, 0) ;Delete Total Rows Value
Dim $Arraytofill[1]
For $i = 0 to UBound($aTxt,1) -1
_ArrayAdd($Arraytofill,$aTxt[$i])
Next    
_ArrayDelete($Arraytofill, 0) ;Delete Total Rows Value

_ArraySort($Arraytofill)
_ArrayDisplay($Arraytofill,"$Arraytofill")

$aTxt2 = StringRegExpReplace($FileRead & @CRLF, "(.*?)(\r\n)(.*?)(\r\n)", "$3,");Ready line and pass line
$aTxt2 = StringSplit($aTxt2, ",");Ready line and pass line
_ArrayDelete($aTxt2, 0) ;Delete Total Rows Value
Dim $final_array[1]
For $i = 0 to UBound($Arraytofill,1)-1
    ;$fileToRead = @ScriptDir & "\daysarray.qu"
ConsoleWrite(@CRLF&$aTxt[$i])
$getLine=_LineNumsOfSearchStr($fileToRead,$Arraytofill[$i])
;ConsoleWrite(@CRLF&"Next Line is: "&$Second_Line)
$Second_Line=FileReadLine($fileToRead,$foundedNUM+1)
_ArrayAdd($final_array,$Second_Line)
Next
_ArrayDelete($final_array, 0) ;
_ArrayDisplay($final_array,"$final_array")
FileDelete($fileToRead)
Sleep(1000)
For $i = 1 to UBound($Arraytofill,1)-1
    ConsoleWrite(@CRLF&$Arraytofill[$i])
    ConsoleWrite(@CRLF&$final_array[$i])
;_ArrayDisplay($final_array,"$final_array") 
    FileWrite($fileToRead,@crlf&$Arraytofill[$i])
    FileWrite($fileToRead,@crlf&$final_array[$i])
Next
 TidyDayssarray()
EndFunc


Func _LineNumsOfSearchStr($sFileName, $searchString, $bDeleteLine = False)

    Local $location, $aCurrentLineNum, $iCurrentLineNum, $sFile, $iOccur = 1, $sRes = ""
    If FileExists($sFileName) = 0 Then Return 1
    Do
        $sFile = FileRead($sFileName)
         $location = StringInStr($sFile, $searchString, 0, $iOccur) ; Find the $iOccur occurrence of the "substring"

        If $location > 0 Then
             $aCurrentLineNum = StringRegExp(StringRegExpReplace($sFile, "(?s)(.{" & $location & "})(.*)$", "\1"), "(?s)(\v+)", 3) ;Find line number
             $iCurrentLineNum = UBound($aCurrentLineNum) + 1 ; Line number
        Global  $foundedNUM=$iCurrentLineNum
;Consolewrite(@crlf&"Founded Date Line Is: "&$foundedNUM&@crlf)
        ;   _FileWriteToLine($sIniFile, 1, $iCurrentLineNum, True)
            ;ConsoleWrite("CharPos: " &  $location & "   Ln: " & $iCurrentLineNum & @CRLF)
            $sRes &= $iCurrentLineNum & "|"
            If $bDeleteLine Then
            Else
                $iOccur += 1
            EndIf
        Else
            ExitLoop
        EndIf
        Sleep(10)
    Until 0
    FileClose($sFile)
    ;ShellExecute($fileToRead)
    Return StringSplit(StringTrimRight($sRes, 1), "|")
    Return $foundedNUM
EndFunc   ;==>_LineNumsOfSearchStr
Func TidyDayssarray()
Global $aLines
_FileReadToArray($fileToRead, $aLines)

For $i = $aLines[0] To 1 Step -1
    If $aLines[$i] = "" Then
        _ArrayDelete($aLines, $i)
    EndIf
Next
_FileWriteFromArray($fileToRead, $aLines, 1)
ConsoleWrite(@crlf& " Tidy Done" & @CRLF)
EndFunc

 

Edited by abdulrahmanok
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...