Jump to content

New to AutoIt, need help with while loop


Recommended Posts

So, I only first heard of AutoIt about 4 days ago, which is amazing because it seems like a very powerful language. But anyway, I'm writing a script that works in conjunction with a c# program. The c# program checks to see if certain programs are installed, and if not downloads them and creates a temp text file. C# then hands off processing to the AutoIt script to read the text file and install the newly downloaded files.

Here's a sample of what file.txt looks like

program1.exe

program2.exe

I'm pretty sure that since I don't really know what I'm doing with AutoIt, I'm not using it with the most efficiency, but for now I just want it to work, it was difficult for me to narrow this down, but it looks like where I'm trying to assign values to the different indexes of the array, nothing is being assigned. Can anyone help, or at least point me in the right direction?

Script below:

#include <Array.au3>

#include <file.au3>

#RequireAdmin

FileChangeDir(@AppDataCommonDir & "\myProgram\Temp\")

If FileExists ("file.txt") <>1 Then

MsgBox(0, "Error", "Text File Not Found")

Exit

EndIf

Const $file = FileOpen ("file.txt",0)

Local $c = 0, $line

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

While 1

$line = FileReadLine($file)

If @error = -1 Then ExitLoop

$c = $c + 1

WEnd

Local $array[$c]

$c = 0

;I believe this while loop is where it breaks

;the second line was only put there to test and see what is in the current variable/array

;It looks like $line is not being assigned anything in the first line

While 1

$line = FileReadLine($file)

MsgBox(0, "Test", string($line))

If @error = -1 Then ExitLoop

$array[$c] = $line

$c = $c + 1

WEnd

FileClose ($file)

$c = 0

;this line, again, is only to let me know if the script made it here without error

local $msgTest = String ($toBeInstalled[$c])

MsgBox (0, "test", $msgTest)

While 1

RunWait ($array[$c])

If @error = -1 Then ExitLoop

$c = $c + 1

WEnd

;this line, again, is only to let me know if the script made it here without error

MsgBox(0,"Fin","Code Completed successfully")

Exit

Link to comment
Share on other sites

  • Moderators

DitoMuertez,

Welcome to the AutoIt forum. :)

You are making rather a meal of all that - it can be done in 3 lines: :P

; Include the UDF
#include <File.au3>
; Declare the array
Local $aArray
; Read the file into the array
_FileReadToArray(@AppDataCommonDir & "\myProgram\Temp\file.txt", $aArray)

And Robert is your mother's brother! :)

Of interest the problem in your original script was that you were running 2 successive loops using FileReadLine. Each time you call FileReadLine with no parameter AutoIt augments the count by 1 which meant that the second loop started reading past the end of the file - thus you never read anything. You needed to reset the count for the second loop. Furthermore you did not really need the first loop anyway - there is a nice little function called _FileCountLines which does that job. :D

But all that is moot as the snippet above shows you how to do it in the easiest way! ;)

Please ask if anything is unclear. :)

M23

P.S. If you post code in the future, please use code tags - put [autoit] before and [/autoit] after the code. ;)

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thank you Sir!

makes perfect sense, i knew i was making it more complicated than it had to be. And the funny thing is, I remember reading somewhere that filereadline() goes concurrently to the next line...hell, I was even using it that way. :).

But yeah, thanks again. Also, about the tags, I knew I was missing something, will use next time.

Link to comment
Share on other sites

A note to add:

Originally, I was using filereadtoarray, but i wasn't sure if it was going to separate the indexes by line or not. At that time, I wasn't sure how to display the contents of the array, which was why I counted and built the way I did.

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