Solved

Reading and Writing variables to a file using AutoIT

Posted on 2011-03-22
5
2,898 Views
Last Modified: 2013-11-13
Hello

 I am trying to create a password vault for storing usernames and passwords.
 using AutoIT.

So far I have:

#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) ; 
WEnd

Func _Exit()
    Exit
EndFunc   ;==>_Exit

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

Open in new window


I then need an Input box where the user types the $program variable and it displays the $username and $password variables in a message box.  I have tried the following to test the iniread but it's not working:

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

Open in new window


Can anybody suggest a better way of doing this?

Many Thanks
0
Comment
Question by:JakeSpencer
  • 3
  • 2
5 Comments
 
LVL 29

Expert Comment

by:matrixnz
ID: 35187888
The @AppDataDir & "\Meerecat\PasswordVault' folder must exist, use something like the following at the top of the script:

If FileExists(@AppDataDir & '\Meerecat\PasswordVault') = 0 Then
  DirCreate(@AppDataDir & '\Meerecat\PasswordVault')
EndIf

0
 

Author Comment

by:JakeSpencer
ID: 35187933
Hello

It does exist, and write's the variables to the ini file as expected.  I just can't work out how to get te information back.

Thanks
0
 
LVL 29

Expert Comment

by:matrixnz
ID: 35187946
Writing something for you now.
0
 
LVL 29

Accepted Solution

by:
matrixnz earned 500 total points
ID: 35188273
Quick Script which I hopefully you can build on.

Cheers
#include <Array.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>

Global $INI_INFO = @AppDataDir & '\Meerecat\PasswordVault\PassVlt.ini'
If FileExists(@AppDataDir & '\Meerecat\PasswordVault') = 0 Then DirCreate(@AppDataDir & '\Meerecat\PasswordVault')

GUICreate("Form", 350, 315)
	GUICtrlCreateLabel('Program / Website', 10, 10, 100, 20)
	$GUI_INFO = GUICtrlCreateInput('', 110, 10, 200, 20)
	GUICtrlCreateLabel('Username', 10, 35, 100, 20)
	$GUI_USER = GUICtrlCreateInput('', 110, 35, 200, 20)
	GUICtrlCreateLabel('Password', 10, 60, 100, 20)
	$GUI_PWRD = GUICtrlCreateInput('', 110, 60, 200, 20)
	$BTN_SAVE = GUICtrlCreateButton('Save', 260, 85, 50, 20)
	GUICtrlCreateLabel('Password List', 10, 110, 100, 20)
	$GUI_READ = GUICtrlCreateCombo('Check', 110, 110, 200, 20, $CBS_DROPDOWNLIST + $CBS_SORT)
GUISetState()

_FNC_READ()

While 1
	$msg = GUIGetMsg()
	Switch $msg
		Case $GUI_EVENT_CLOSE
			Exit
		Case $BTN_SAVE
			IniWrite($INI_INFO, GUICtrlRead($GUI_INFO), 'USERNAME', GUICtrlRead($GUI_USER))
			IniWrite($INI_INFO, GUICtrlRead($GUI_INFO), 'PASSWORD', GUICtrlRead($GUI_PWRD))
			_FNC_READ()
		Case $GUI_READ
			If GUICtrlRead($GUI_READ) <> 'Check' Or GUICtrlRead($GUI_READ) > 0 Then
				GUICtrlSetData($GUI_INFO, GUICtrlRead($GUI_READ))
				GUICtrlSetData($GUI_USER, IniRead($INI_INFO, GUICtrlRead($GUI_READ), 'USERNAME', 'UNKNOWN'))
				GUICtrlSetData($GUI_PWRD, IniRead($INI_INFO, GUICtrlRead($GUI_READ), 'PASSWORD', 'UNKNOWN'))
			EndIf
			If GUICtrlRead($GUI_READ) <> 'Check' Or GUICtrlRead($GUI_READ) > 0 Then GUICtrlCreateEdit(IniRead($INI_INFO, GUICtrlRead($GUI_READ), 'USERNAME', 'UNKNOWN') & @CRLF & IniRead($INI_INFO, GUICtrlRead($GUI_READ), 'PASSWORD', 'UNKNOWN'), 110, 135, 200, 100)
	EndSwitch
WEnd

Func _FNC_READ()
	Local $INI_READ
	Local $VAR_READ
	If FileExists($INI_INFO) Then
		$INI_READ = IniReadSectionNames($INI_INFO)
		If IsArray($INI_READ) Then
			$VAR_READ = _ArrayToString($INI_READ, '|', 1)
			GUICtrlSetData($GUI_READ, $VAR_READ)
		EndIf
	EndIf
EndFunc

Open in new window

0
 

Author Comment

by:JakeSpencer
ID: 35188348
Hello

Many thanks for this.  Gives me a place to start and plenty to work from.

Many thanks again
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A short article about problems I had with the new location API and permissions in Marshmallow
This is about my first experience with programming Arduino.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now