Jump to content

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


Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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...