Solved

Reading and Writing variables to a file using AutoIT

Posted on 2011-03-22
5
3,018 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

738 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