Solved

Reading and Writing variables to a file using AutoIT

Posted on 2011-03-22
5
2,939 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

776 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