abdulrahmanok

Read file text to Excil and bybass line part (Solved)

7 posts in this topic

#1 ·  Posted (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 by abdulrahmanok

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Finally after few Hours of searching I got a little bit tricky way to do this:

#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 by abdulrahmanok

Share this post


Link to post
Share on other sites

#3 ·  Posted

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

1 person likes this

Share this post


Link to post
Share on other sites

#4 ·  Posted (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.

Array.png

that's my fault I forgot to add &crlf after "file read"

Edited by abdulrahmanok

Share this post


Link to post
Share on other sites

#5 ·  Posted

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,")

Share this post


Link to post
Share on other sites

#6 ·  Posted

Did you try what you propose? It will not work:D.

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.

Share this post


Link to post
Share on other sites

#7 ·  Posted

20 hours ago, junkew said:

Did you try what you propose? It will not work:D.

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.

Share this post


Link to post
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