Solved

Reading and Writing variables to a file using AutoIT

Posted on 2011-03-22
5
3,043 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
Make the most of your online learning experience.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

717 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