Search code examples
stringvariablestext-filesautoit

How to get one variable to equal multiple lines of a .txt file?


How to get one variable to update its string value every loop? I am storing lines of a text file in a variable named $attempt, then I show it in a textbox.

The .txt file:

pw.txt

My AutoIt script:

AutoIt Picture

$attempt always equals string "dog" (first line of pw.txt), which means "dog" is entered in the textbox 5 times. I hope to get "Dog", "Apple", "Sand", "Shoes", and "Hat" entered in the textbox, all once, on their own, at different times.

How to get variable $attempt to equal the following line in "pw.txt" after each loop iteration (for example,

  1. first iteration $attempt = "Dog",
  2. second iteration $attempt = "Apple",
  3. third iteration $attempt = "Sand", etc.)?

Lines of text are examples, subject to change.


Solution

  • You need to use a file handle. At the moment you are opening the file, reading the file and closing it in each iteration of the loop. By using a file handle, the file is kept open and the next line is read.

    e.g.

    $fh=FileOpen("pw.txt")
    for $i=1 to 5
     $attempt=FileReadLine($fh)
     MsgBox(4096,"test",$attempt)
    Next