Jump to content

Loop through lines in csv file


Recommended Posts

Hi all, n00bie here :)

 

I need some help, if you please; I'm trying to make a script that listens to entered keywords on web page, then finds a specific line that's stored in csv (or text) file and open a new window via url from 2nd text file, while script is running nonstop (while 1, sleep 100).

Currently, it works but it reads only the 1st line. I've tried with FileReadToArray, but no luck. 

So, could you pls help me? Thanks a lot!

Here's the code:

while 1
sleep(100)

$Line=1
$path="strings.csv"
$searchstring1=FileReadLine($path,$Line)

For $i = 1 to _FileCountLines($path)
    $searchstring1 = FileReadLine($path, $i)
;msgbox(0,'','the line ' & $i & ' is ' & $line)
Next

$Line2=2
$path2="links.csv"
$myurl=FileReadLine($path2,$Line2)

For $i = 1 to _FileCountLines($path2)
    $myurl = FileReadLine($path2, $i)
;msgbox(0,'','the line ' & $i & ' is ' & $line)
Next


$title = WinGetTitle("")

If StringInStr($title, $searchstring1) Then
;do some stuff here

EndIf


wend

 

Link to comment
Share on other sites

Hi.

open a handle to read the file, then read line by line until the EOF is reached:

 

$File="C:\temp\myfile.csv"

; create sample CSV file
$h=FileOpen($File,2+8)
for $i = 1 to 10
    $NL="Line-" & $i & ";column2 line-" & $i & ";" & $i^2
    FileWriteLine($h,$NL)
Next
FileClose($h)

; open for read only
$h=FileOpen($File,0)
while 1
    $NextLine=FileReadLine($h)
    if @error Then
        FileClose($h)
        ExitLoop
    Else
        ConsoleWrite("Reading next line:" & $NextLine & @CRLF)
    EndIf
WEnd

MsgBox(0, '', "done")

Regards, Rudi.

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

Just general tip for optimalization:

If your CSV files doesn't change during your loop then avoid reading+parsing them inside loop repeatedly.

Instead read them (FileRead+StringSplit) into variable type of array before main loop and inside loop only go through these (two) arrays.

 

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