abdulrahmanok Posted August 28, 2017 Share Posted August 28, 2017 (edited) welcome again brothers, In this topic I was trying to read Text file to array but bypass some lines which mean read line 2 then 4 then 6.... and the solution was the @junkew code: #include <Excel.au3> #include<file.au3> $fileToRead="DaysArray.txt" ; Read the contents of the file using the handle returned by FileOpen. Local $strFileRead = FileRead($fileToRead) ;~ consolewrite("Initial file contents" & @CRLF) ;~ consolewrite("=====================" & @CRLF) ;~ consolewrite($strFileRead & @CRLF) $strFileRead=stringregexpreplace($strFileRead,"(.*?\r\n)(.*)(\r\n)?","$2,") $Array=stringsplit ($strFileRead,",") _ArrayDisplay( $Array,"Four matches, split apart, as expected") consolewrite("Only every 2nd line" & @CRLF) consolewrite("=====================" & @CRLF) consolewrite($strFileRead & @CRLF) now I want the reverse I need to read lines singly Line 1 then line 3 ,line 5...... I tried this: $strFileRead2=stringregexpreplace($strFileRead,"(.*?\r\n)(.*)(\r\n)?","$1,") ; I thought that I must change (2) to make difference $Array2=stringsplit ($strFileRead,",") I also can't understand how Stringregreplace work even after looking at this :https://www.autoitscript.com/autoit3/docs/functions/StringRegExp.htm Edited August 28, 2017 by abdulrahmanok Link to comment Share on other sites More sharing options...
abdulrahmanok Posted August 28, 2017 Author Share Posted August 28, 2017 (edited) Finally after few Hours of searching I got a little bit tricky way to do this: expandcollapse popup#include <Excel.au3> #include <file.au3> ProcessClose("Excel.exe") ;;;;;;;;;;;;;; Compare Between DaysArray And DaysArray And Add "None" If @error Then Else $fileToRead="DaysArray.txt" ; Read the contents of the file using the handle returned by FileOpen. Local $FileRead = FileRead($fileToRead) ;~ consolewrite("Initial file contents" & @CRLF) ;~ consolewrite("=====================" & @CRLF) ;~ consolewrite($strFileRead & @CRLF) $strFileRead=stringregexpreplace($FileRead,"(.*?\r\n)(.*)(\r\n)?","$2,") $Array=stringsplit ($strFileRead,",") $strFileRead2=stringregexpreplace($FileRead,"\n",",") $Array2=stringsplit ($strFileRead2,",") For $i = UBound($Array2,1)-1 to 1 Step -2 For $x = UBound($Array2)-1 to 1 Step -2 _ArrayDelete($Array2, $i) ExitLoop Next Next _ArrayDelete($Array, 0) ;Delete Total Rows Value _ArrayDelete($Array2, 0) ;Delete Total Rows Value _ArrayDisplay( $Array,"Array 1") _ArrayDisplay( $Array2,"Array 2") $oExcel = ObjCreate("Excel.Application") $oExcel.Visible = 1 Local $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir & "\Excel.xlsx") Sleep(100) _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $Array, "C2") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $Array2, "A2") ;ProcessClose("Excel.exe") EndIf Solved, Thanks For all Autoit forum Users who giving me a lot of experience.some references: https://www.autoitscript.com/autoit3/docs/keywords/For.htm Edited August 28, 2017 by abdulrahmanok Link to comment Share on other sites More sharing options...
junkew Posted August 28, 2017 Share Posted August 28, 2017 Try this Local $FileRead = FileRead($fileToRead) & @crlf $strFileRead=stringregexpreplace($FileRead,"(.*?)(\r\n)(.*?)(\r\n)","$3,") Regex parts are between braces () It splits every 2 lines in 4 parts and only copies the 3rd with a comma appended excel close you should use https://www.autoitscript.com/autoit3/docs/libfunctions/_Excel_Close.htm Procesclose is not a good solution Try https://www.autoitscript.com/autoit3/docs/libfunctions/_Excel_Open.htm abdulrahmanok 1 FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets Link to comment Share on other sites More sharing options...
abdulrahmanok Posted August 28, 2017 Author Share Posted August 28, 2017 (edited) Firstly ,thanks for advice's I will keep it in my mind, and about this code: Local $FileRead = FileRead($fileToRead) & @crlf $strFileRead=stringregexpreplace($FileRead,"(.*?)(\r\n)(.*?)(\r\n)","$3,") It's do the job but there is a little bug mentioned in attached image. that's my fault I forgot to add &crlf after "file read" Edited August 29, 2017 by abdulrahmanok Link to comment Share on other sites More sharing options...
abdulrahmanok Posted August 29, 2017 Author Share Posted August 29, 2017 and why you duplicate write : (\r\n)(.*?) why code don't be like this: $strFileRead=stringregexpreplace($FileRead,"(.*?)(\r\n)","$3,") Instead of this: $strFileRead=stringregexpreplace($FileRead,"(.*?)(\r\n)(.*?)(\r\n)","$3,") Link to comment Share on other sites More sharing options...
junkew Posted August 29, 2017 Share Posted August 29, 2017 Did you try what you propose? It will not work. You propose a 2 part regex to replace with the third part that does not exist. A regular expression can be single or multipart and the $n part refers to the matched part. Google on regular expression for more background. FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets Link to comment Share on other sites More sharing options...
abdulrahmanok Posted August 30, 2017 Author Share Posted August 30, 2017 20 hours ago, junkew said: Did you try what you propose? It will not work. You propose a 2 part regex to replace with the third part that does not exist. A regular expression can be single or multipart and the $n part refers to the matched part. Google on regular expression for more background. Thanks again , that you taught me much again. 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