Jump to content

Reading and Writing variables to a file


Recommended Posts

Hello

I am sure this has been discussed before. However I am very new to Autoit and am struggling to make sense of everything. I am trying to create a password vault for storing usernames and passwords.

So far I can create a password and write it to an ini file.

What I am struggling with is the latter part, taking 3 input boxes and writing them to the ini file.

#NoTrayIcon
#include <GUIConstantsEx.au3>

Global $ini = @AppDataDir & "\Meerecat\PasswordVault\PassVlt.ini"
Opt("GUIOnEventMode", 1); 
If Not FileExists($ini) Then
    $hnd = FileOpen($ini, 9)
    FileWrite($hnd, "[Password]" & @CRLF)
    FileWriteLine($hnd, "Password=")
    FileClose($hnd)
    MsgBox(0 + 64, "Meerecat Folder Lock", "This is your first time running Folder Lock on this PC" & @LF & @LF & "You will now be prompted to create a password.")
    $new = 1
    _ChangePW()
    
EndIf   

    
    $bLoop = 1
While $bLoop = 1
    $text = InputBox("Meerecat Folder Lock", "Please type your password and click OK.", "", "*")
    If @error = 1 Then Exit
    If $text <> IniRead($ini, "Password", "Password", "failed") Then
        MsgBox(4096, "Error", "Incorrect Password - try again!")
    Else
        $bLoop = 0 ; 
    EndIf
WEnd
$new = 0

Func _Exit()
    Exit
EndFunc

Func _ChangePW()
    If $new = 0 Then
        $bLoop = 1
        While $bLoop = 1
            $text = InputBox("Meerecat Folder Lock", "Please type your password and click OK.", "", "*")
            If @error = 1 Then Exit
            If $text <> IniRead($ini, "Password", "Password", "failed") Then
                MsgBox(4096, "Error", "Incorrect Password - try again!")
            Else
                $bLoop = 0 
            EndIf
        WEnd
    EndIf
    $pw1 = InputBox("Meerecat Folder Lock", "Please create a new password and click OK.", "", "*")
    If @error = 1 Then Exit
    $pw2 = InputBox("Meerecat Folder Lock", "Please confirm your new password and click OK.", "", "*")
    If @error = 1 Then Exit
    While $pw1 <> $pw2
        $pw1 = InputBox("Meerecat Folder Lock", "Passwords do not match or are blank. Please try again.", "", "*")
        If @error = 1 Then Exit
        $pw2 = InputBox("Meerecat Folder Lock", "Please confirm your new password and click OK.", "", "*")
        If @error = 1 Then Exit
        If $pw2 = "" Then $pw2 = "ThisPreventsBlankPasswords"
    WEnd
    IniWrite($ini, "Password", "Password", $pw2)
    MsgBox(0 + 64, "Meerecat Folder Lock", "Your new password has been set.")
EndFunc   ;==>_ChangePW

$Form1 = GUICreate("Form1", 228, 215, 192, 124)
$program = GUICtrlCreateInput("", 8, 32, 169, 21)
$Label1 = GUICtrlCreateLabel("Program / Website", 8, 56, 93, 17)
$username = GUICtrlCreateInput("", 12, 84, 169, 21)
$Label2 = GUICtrlCreateLabel("Username", 16, 113, 52, 17)
$password = GUICtrlCreateInput("", 12, 141, 169, 21)
$Label3 = GUICtrlCreateLabel("Password", 12, 170, 50, 17)
$button = GUICtrlCreateButton("Save", 12, 180, 50, 17)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
GUISetState(@SW_SHOW)


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

Using the above code, I need to

1. Write the $program, $username and $password to the ini file created earlier in the script.

2. Have the $username and $password displayed when somebody enters the $program into a new input box I am yet to create.

I certainly don't expect anybody to write the script for me, but would be greatful for some pointers on how to start.

Many Thanks

M

Lack of planning on your part does not constitute an emergency on my part.-The biggest idiot can ask questions the smartest man cannot answer.

Link to comment
Share on other sites

Thanks for this.

I have tried this code:

#include <GUIConstantsEx.au3>
Global $ini = @AppDataDir & "\Meerecat\PasswordVault\PassVlt.ini"
Opt("GUIOnEventMode", 1)

GUICreate("Form1", 228, 215, 192, 124)
$program = GUICtrlCreateInput("", 8, 32, 169, 21)
$Label1 = GUICtrlCreateLabel("Program / Website", 8, 56, 93, 17)
$username = GUICtrlCreateInput("", 12, 84, 169, 21)
$Label2 = GUICtrlCreateLabel("Username", 16, 113, 52, 17)
$password = GUICtrlCreateInput("", 12, 141, 169, 21)
$Label3 = GUICtrlCreateLabel("Password", 12, 170, 50, 17)
$button = GUICtrlCreateButton("Save", 12, 180, 50, 17)
GUICtrlSetOnEvent($button, "_Write")
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
GUISetState(@SW_SHOW)
While 1
    Sleep(1000) ;
WEnd

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Func _Write()
IniWrite ( $ini, $program, $username, $password )
EndFunc

However no matter what data I put in these fields, the ini file always shows:

[3]

5=7

What have I done wrong?

Many Thanks

M

Lack of planning on your part does not constitute an emergency on my part.-The biggest idiot can ask questions the smartest man cannot answer.

Link to comment
Share on other sites

you are not saving the values but the controlIDs of each control.

Try:

IniWrite ( $ini, GUICtrlRead($program), GUICtrlRead($username), GUICtrlRead($password ))

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Thank you. I am now trying to read the file back, but this is not working either.

#include <GUIConstantsEx.au3>
Global $ini = @AppDataDir & "\Meerecat\PasswordVault\PassVlt.ini"
Opt("GUIOnEventMode", 1)

GUICreate("Form1", 228, 215, 192, 124)
$program = GUICtrlCreateInput("", 8, 32, 169, 21)
$Label1 = GUICtrlCreateLabel("Program / Website", 8, 56, 93, 17)
$username = GUICtrlCreateInput("", 12, 84, 169, 21)
$Label2 = GUICtrlCreateLabel("Username", 16, 113, 52, 17)
$password = GUICtrlCreateInput("", 12, 141, 169, 21)
$Label3 = GUICtrlCreateLabel("Password", 12, 170, 50, 17)
$button = GUICtrlCreateButton("Save", 12, 180, 50, 17)
$read = GUICtrlCreateButton("Read", 12, 200, 50, 17)
GUICtrlSetOnEvent($button, "_Write")
GUICtrlSetOnEvent($read, "_Read")
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
GUISetState(@SW_SHOW)
While 1
    Sleep(1000) ; =======================================================================while loop here.
WEnd

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Func _Write()
IniWrite ( $ini, GUICtrlRead($program), GUICtrlRead($username), GUICtrlRead($password ))
EndFunc


Func _Read()
    $read = IniRead  ( $ini, GUICtrlRead($program), GUICtrlRead($username), GUICtrlRead($password ))
MsgBox(4096, "Result", $read)
EndFunc

Ultimately I need an input box that will match the [section] name in the ini and then display the Key and Value.

Perhaps I should accept this is above me, but it sounded so simple when I started :)

Lack of planning on your part does not constitute an emergency on my part.-The biggest idiot can ask questions the smartest man cannot answer.

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