Jump to content

FileReadLine problem


Recommended Posts

Hey I watched out the FileReadLine, I want to put settings into a text file.

So this code, if I understand it correctly, it takes all the lines in the text file "test.txt" that's located in the same folder as the AutoIT script.

That means if I put $test = 2 in the test.txt, it will work? Because in the original example it was sending msgbox for each line, with the text inside that msgbox for each line.

So I tried this out, but I can't get it to work ;)

; Script Start - Add your code below here
Local $file = FileOpen("test.txt", 0)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
; Read in lines of text until the EOF is reached
While 1
    Local $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
WEnd
FileClose($file)
If $test = 2 Then
   msgbox(0,"Success","Success!")
Else
   msgbox(0,"FAIL", "FAIL!")
EndIf

The error occurs at the $test

It says I haven't declared it. Even though it shall be from the test.txt file?

Or do I have to declare it in the code, so it points to the text file? how would that be done? :S

thanks in advance.

Link to comment
Share on other sites

Still don't completely understand what your are trying to achieve, however I don't believe you can just use the variable test without declaring it anywhere in the code previously. Try adding $test = "" somewhere.

Edit: I think what you may be after is a ini file, read up on IniRead & IniWrite.

Edited by Venix
Link to comment
Share on other sites

Still don't completely understand what your are trying to achieve, however I don't believe you can just use the variable test without declaring it anywhere in the code previously. Try adding $test = "" somewhere.

Tried add that, and got up the fail msgbox.

What I want to achieve is to be able to choose and change settings easily in a text file. I want to be able to put the values of variables in the text file.

In the textfile I want to write for example:

$coord_X = 500

$coord_Y = 400

$name = jesus

$num = 5000

or something.

Then I want to be able to have those values into the code.

So if I would write a msgbox it could be

MsgBox(0,"Number", $num)

and there I would get 5000 because in the textfile I'd $num = 5000

By this I don't need to get in the script file to change variables if needed.

I also then can use the setting text file for multiple AutoIT script file and don't have to keep copy all variables with their value to every script file.

Link to comment
Share on other sites

Try something like this it uses ini files then you can open up the file in notepad, it will do exactly what your after.

If Not FileExists(@ScriptDir & "Settings.ini") Then
IniWrite("Settings.ini", "TestSection", "Test", "2") ; Create/Write to an inifile called settings.
EndIf
$Test = IniRead("Settings.ini", "TestSection", "Test","1") ; Read the value of test in the inifile.
If $Test = 2 Then
   MsgBox(64, "IniRead", "Yay")
Else
   MsgBox(64, "IniRead", "Oh no :(")
EndIf

Mess about with this code.

Edited by Venix
Link to comment
Share on other sites

Try something like this it uses ini files then you can open up the file in notepad, it will do exactly what your after.

If Not FileExists(@ScriptDir & "Settings.ini") Then
IniWrite("Settings.ini", "TestSection", "Test", "2") ; Create/Write to an inifile called settings.
EndIf
$Test = IniRead("Settings.ini", "TestSection", "Test","1") ; Read the value of test in the inifile.
If $Test = 2 Then
   MsgBox(64, "IniRead", "Yay")
Else
   MsgBox(64, "IniRead", "Oh no :(")
EndIf

Mess about with this code.

Aah yea ;)

I was just reading about iniRead (from AutoIT's wiki tutorial) and it works great.

Thanks :]

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