Jump to content

reading from and saving to a text file


Recommended Posts

OK, here is a stripped version of my script. This script works without errors, but either _FileWriteToLine does not accept variables as input or it doesn't like lines such as $var = GUICtrlCreateInput($var, 1,1,1,1) where I tell it to remake the variable (didnt make much since there but if you look at the script you see what is trying to be accomplished.).

The first script is the au3 file, the second script is the accompanying settings.txt file. I provided a ZIP below.

My goal for the script is for the checkbox toggle (i dont know where to start with that), program name, version, and the path be saved in the text file and can be edited and saved again through the GUI. probably simple but once you get all those variables I start to get confused. :whistle: Plus there are going to be 25+ of these in the full version, once I get this small one working and change variables.

thanks in advance, I love this program it's like a giant puzzle getting everything to work right! It's really fun IMO. :)

#include<file.au3>
#include <GUIConstants.au3>

$program_name  =    FileReadLine("settings.txt", 1)
$program_check =    FileReadLine("settings.txt", 2)
$program_version =  FileReadLine("settings.txt", 3)
$program_path =     FileReadLine("settings.txt", 4)

GUICreate("Example", 300, 200)
$program_checkbox = GUICtrlCreateCheckbox($program_name, 5, 30, 15, 15)
If $program_check = 1 Then GUICtrlSetState(-1, $GUI_CHECKED)
If $program_check = 0 Then GUICtrlSetState(-1, $GUI_UNCHECKED)
$program_name = GUICtrlCreateInput($program_name, 25,  25, 125, 25)
$program_version = GUICtrlCreateInput($program_version, 150,  25, 75, 25)
$program_path_button = GUICtrlCreateButton("Path", 230, 25, 50, 25)
$save = GUICtrlCreateButton("Save Changes", 25, 150, 100, 25)
GUISetState()

While 1
    $msg = GUIGetMsg()
    If $msg = $save Then
        FileOpen("settings.txt", 2)
        _FileWriteToLine("settings.txt", 1, $program_name, 1)
        _FileWriteToLine("settings.txt", 3, $program_version, 1)
                _FileWriteToLine("settings.txt", 4, $program_path, 1)
        If @error > 0 Then
            MsgBox(0, "Sorry", "File settings.txt could not be written to.  You may have used incorrect syntax or the file may be corrupt or missing.")
        Else
            MsgBox(0, "Successful", "Saved Successfully.", 5)
        EndIf
    ElseIf $msg = $GUI_EVENT_CLOSE Then
        Exit
    ElseIf $msg = $program_path_button Then
        InputBox("New Path", "Enter the path to the program.", $program_path, "", 250, 100)
    EndIf
WEnd

Program Name
1
1.1.1.0
C:\Program Files\Program1\Program.exe

example.zip

Edited by mike1305

Amp Energy Drink: the official sponsor of me scripting at 2AM.

Link to comment
Share on other sites

hold up, I don't think the files in the ZIP are correct. :">

Allright, should be set now. In my excitement to pose the question I got some stuff mixed up.

Edited by mike1305

Amp Energy Drink: the official sponsor of me scripting at 2AM.

Link to comment
Share on other sites

You should try using Ini functions (IniRead, IniWrite and IniReadSectionNames) instead of a text file.

It's much easier and Autoit handles the lines and stuff, so you can have

as much values you want.

[quote name='Valik' post='301213' date='Jan 31 2007, 10:36 PM']You seem to have a habit of putting things in the wrong place. I feel sorry for any female you attempt to have sex with.[/quote][font="Lucida Sans Unicode"][/font]

Link to comment
Share on other sites

You should try using Ini functions (IniRead, IniWrite and IniReadSectionNames) instead of a text file.

It's much easier and Autoit handles the lines and stuff, so you can have

as much values you want.

Cool. I will tinker around with that for a bit to see if it solves the problem. thanks for the quick response!

Amp Energy Drink: the official sponsor of me scripting at 2AM.

Link to comment
Share on other sites

OK, Here's the new issue. My script can read the ini but when it comes to writing it it doesnt do it right.

It always sets the version to 5, the name to 4, and doesnt save the path at all.

#include<file.au3>
#include <GUIConstants.au3>

$program_name  =    IniRead("settings.ini", "Program1", "Program_Name", "NotFound")
$program_check =    IniRead("settings.ini", "Program1", "Check", "NotFound")
$program_version =  IniRead("settings.ini", "Program1", "Version", "NotFound")
$program_path =     IniRead("settings.ini", "Program1", "Path", "NotFound")

GUICreate("Example", 300, 200)
$program_checkbox = GUICtrlCreateCheckbox($program_name, 5, 30, 15, 15)
If $program_check = 1 Then GUICtrlSetState(-1, $GUI_CHECKED)
If $program_check = 0 Then GUICtrlSetState(-1, $GUI_UNCHECKED)
$program_name = GUICtrlCreateInput($program_name, 25,  25, 125, 25)
$program_version = GUICtrlCreateInput($program_version, 150,  25, 75, 25)
$program_path_button = GUICtrlCreateButton("Path", 230, 25, 50, 25)
$save = GUICtrlCreateButton("Save Changes", 25, 150, 100, 25)
GUISetState()

While 1
    $msg = GUIGetMsg()
    If $msg = $save Then
        IniDelete("settings.ini", "Program1", "Program_Name")
        IniWrite("settings.ini", "Program1", "Program_Name", $program_name)
        IniDelete("settings.ini", "Program1", "Version")
        IniWrite("settings.ini", "Program1", "Version", $program_version)
        IniDelete("settings.ini", "Program1", "Path")
        IniWrite("settings.ini", "Program1", "Path", $program_path)
    ElseIf $msg = $GUI_EVENT_CLOSE Then
        Exit
    ElseIf $msg = $program_path_button Then
        InputBox("New Path", "Enter the path to the program.", $program_path, "", 250, 100)
    EndIf
WEnd

the ini as I set it up:

[program1]
Program_Name=Program1
Check=1
Version=2.0.0.0
Path=C:\Program Files\Program\program.exe

the ini after the program saves changes:

[program1]
Check=1
Program_Name=4
Version=5
Path=C:\Program Files\Program\script.au3

Amp Energy Drink: the official sponsor of me scripting at 2AM.

Link to comment
Share on other sites

Try replacing:

IniWrite("settings.ini", "Program1", "Program_Name", $program_name)

With

IniWrite("settings.ini", "Program1", "Program_Name", GUICtrlRead($program_name))

Since $program_name is an Input control, you should read it.

[quote name='Valik' post='301213' date='Jan 31 2007, 10:36 PM']You seem to have a habit of putting things in the wrong place. I feel sorry for any female you attempt to have sex with.[/quote][font="Lucida Sans Unicode"][/font]

Link to comment
Share on other sites

Try replacing:

IniWrite("settings.ini", "Program1", "Program_Name", $program_name)

With

IniWrite("settings.ini", "Program1", "Program_Name", GUICtrlRead($program_name))

Since $program_name is an Input control, you should read it.

AMAZING! thank you a million!!!!!!!

Amp Energy Drink: the official sponsor of me scripting at 2AM.

Link to comment
Share on other sites

  • 5 years later...

It didn't work for me :mellow:. This is my script:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Dim $input, $achtergrond, $positie
#Region ### START Koda GUI section ### Form=c:\documents and settings\jelle besseling\my documents\koda_1.7.3.0\forms\achtergrond veranderen.kxf
$Form1_1 = GUICreate("Selecteer een bestand", 379, 177, 231, 214)
$Label1 = GUICtrlCreateLabel("Selecteer een afbeelding voor de achtergrond, dit moet een bitmap zijn.", 16, 8, 340, 17)
$input = GUICtrlCreateInput("", 16, 40, 249, 21)
$Button1 = GUICtrlCreateButton("Bladeren...", 280, 40, 75, 25)
$Button2 = GUICtrlCreateButton("Instellen", 248, 136, 107, 25)
$Radio1 = GUICtrlCreateRadio("Uitrekken", 16, 88, 113, 17)
$Radio2 = GUICtrlCreateRadio("Vermenigvuldigen", 16, 112, 113, 17)
$Radio3 = GUICtrlCreateRadio("Centreren", 16, 136, 113, 17)
$Group1 = GUICtrlCreateGroup("Positie:", 8, 72, 121, 89)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Button3 = GUICtrlCreateButton("Standaard", 144, 136, 89, 25)
$Button4 = GUICtrlCreateButton("Laatst gebruikt", 144, 104, 89, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit
  Case $Button4
   $Laatst=IniRead ("AVSettings.ini", "LastUsed", "LastUsed", "")
  RegWrite("HKEY_CURRENT_USER\Control Panel\Desktop", "Wallpaper", "REG_SZ", $Laatst)
  Case $Button3
   $achtergrond = ""
  Case $Radio1
   $positie=2
  Case $Radio2
   $positie=1
  Case $Radio3
   $positie=0  
  Case $Button1
   $achtergrond=FileOpenDialog ( "Selecteer een achtergrond", @DesktopDir, "Bitmap (*.bmp)", "1" )
   GUICtrlSetData($input, $achtergrond)     ;set input data
            ;GUICtrlSetData($output, $File2open)    ;same as above
  Case $Button2
  RegWrite("HKEY_CURRENT_USER\Control Panel\Desktop", "Wallpaper", "REG_SZ", $achtergrond)
  RegWrite("HKEY_CURRENT_USER\Control Panel\Desktop", "TileWallaper", "REG_SZ", 0)
  RegWrite("HKEY_CURRENT_USER\Control Panel\Desktop", "WallpaperStyle", "REG_SZ", $positie)
  IniDelete("AVSettings.ini", "LastUsed", "LastUsed" )
  IniWrite("AVSettings.ini", "LastUsed", "LastUsed", GUICtrlRead($input)) ;<--- This is the ini part.
  $func="UpdatePerUserSystemParameters"
  RunWait("c:\windows\system32\rundll32 USER32.DLL," & $func)
  If @Error = NOT 0 Then
   MsgBox (16, "Error!", "Error! U heeft waarschijnlijk een bestand ingevoerd dat niet bestaat!")
  Else
  MsgBox ("64", "", "De achtergrond is ingesteld!")
  Exit
  EndIf
EndSwitch
WEnd

(Sorry it's Dutch)

But it doesn't write a ini at all!

Can somebody please help me?

Link to comment
Share on other sites

Hello JellE,

first, it would be better to create a new thread instead of resurrecting an old thread.

second, You'll need to check whether GuiCtrlread($input) is not empty. Also I'd check what return code IniWrite has.

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
Share on other sites

Hello JellE,

first, it would be better to create a new thread instead of resurrecting an old thread.

second, You'll need to check whether GuiCtrlread($input) is not empty. Also I'd check what return code IniWrite has.

Thank you, I'll make a new thread, and also check what returncode IniWrite have.
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...