help with IniRead

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
G_Allen

help with IniRead

20 Sep 2017, 21:23

I have an .ini file with the following
[Gary Miller]
Subject = Gary Miller
Text_Body = Your Message.

I have tried to read this file with the following IniRead line.
IniRead, Value, %INIFile%, %Subject%, %Field1%,
IniRead, Val, %INIFile%, %Body%, %Field2%,

I get the results of %Value% %Val% in a MsgBox as Gary Miller Gary Miller

What am doing wrong?
Gary Miller and Your Message.
User avatar
boiler
Posts: 16977
Joined: 21 Dec 2014, 02:44

Re: help with IniRead

20 Sep 2017, 22:34

Have you assigned values to variables named INIFile, Subject, Field1, Body, adn Field2? Can you post that part of your script?
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: help with IniRead

20 Sep 2017, 22:41

Where are you defining the values of %INIFILE%, %Subject%, %Body%, %Field1%, and %Field2% ?

Does this result in the correct MsgBox?

Code: Select all

IniRead, Value, %INIFile%, Gary Miller, Subject
IniRead, Val, %INIFile%, Gary Miller, Text_Body
MsgBox %Value% %Val%
?

My guess is that the variables other than INIFILE may be undefined. If those variables are undefined, then you're essentially doing IniRead, Value, %INIFILE% and so it will retrieve a list of all of the Section Names (the ones in [ ]). Since there's only one section in there, it gets the value Gary Miller.

Thus, you can get your code to work (though untested) like this:

Code: Select all

INIFile:=YourFile.ini
Subject:="Gary Miller"
Field1:="Subject"
Body:="Gary Miller"
Field2:="Text_Body"
IniRead, Value, %INIFile%, %Subject%, %Field1%
IniRead, Val, %INIFile%, %Body%, %Field2%
MsgBox %Value% %Val%
But you can see that's terribly confusing to name your variables like that and use multiple variables for the same value. But I hope it sheds some light on the issue for you to play around with it.
Guest

Re: help with IniRead

20 Sep 2017, 22:56

Here is my script.
I input the Subject and Text Body in the Edit boxes and the Write button produces:
[Gary Miller]
Subject = Gary Miller
Text_Body = Message here

Code: Select all

#SingleInstance, Force

/*
Sections = 
(
A = Subject
B = Body

)

 Keys =
 (
 %field1%
 %field2%

 )
*/
INIFile := "myProj.ini"


Gui, Add, Text, x10				w90		h20						, Subject ;the subject line of your text. 1st line shown in text message
Gui, Add, Text, x+10			wp30	hp						, Text body ;your message
Gui, Add, Edit, x10		y+10	wp		hp		vField1 		, Subject
Gui, Add, Edit, x+10	y36		w180	hp		vField2 		, Text body
Gui, Add, Button,x10	y+5		w70		h20		gWrite  		, Write
Gui, Add, Button,x10	y+10		w70		h20		gRead 		, Read
Gui, Show,, `t ; no title
Return

Write:
GUI, Submit, NoHide
INIWrite,%      "`tSubject`t`t= "      Field1  "`n"
           .    "`tText_Body`t= " Field2  "`n"
           
           ,% INIFile,% Field1 ; using 'cosmetical' tabs for better readabilty, feel free to remove'm all :)

;MsgBox, %Field1%  %Field2%
return
Read:

;IniRead, OutputVar, Filename, Section, Key [, Default]
IniRead, Value, %INIFile%, %Subject%, %Field1%, 
IniRead, Val, %INIFile%, %Body%, %Field2%, 
IniRead,  OutputVar,  INIFile, Gary, Field1
MsgBox, The value is %Value% %Val%.
Return
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: help with IniRead

21 Sep 2017, 00:31

Yeah, you never defined a variable "Subject" nor "Body". I suppose when the Section parameter is blank, it ignores the Key parameter and just treats this as a "OutputSectionNames" syntax for IniRead.

And your IniRead is just not matching the syntax anyway. I think you are confused on this because you are using variables as strings and putting them in the wrong places. Let's break down your IniWrite:
INIWrite,% "`tSubject`t`t= " Field1 "`n"
. "`tText_Body`t= " Field2 "`n"

,% INIFile,% Field1 ; using 'cosmetical' tabs for better readabilty, feel free to remove'm all :)
You have written Keys Subject and Text_Body. You have written Values %Field1% and %Field2% (the contents of those variables come from the GUI). You have written Ini File myProj.ini. You have written Section %Field1% (the contents of this variable comes from the GUI -- instructed to be "Subject").

The syntax for IniRead is IniRead, OutputVar, Filename, Section, Key.

This means, if we were to read the first Value in the .ini, we need to specify: IniRead, Value, %INIFile%, %Field1%, Subject. Notice this is not %Subject%, but the literal string Subject.
G_Allen

Re: help with IniRead

21 Sep 2017, 12:28

that works for the 1st value of Field1
I then added
IniRead, Val, %INIFile%, %Field2%, Text_Body to get the Field2 value and get ERROR
User avatar
boiler
Posts: 16977
Joined: 21 Dec 2014, 02:44

Re: help with IniRead

21 Sep 2017, 14:57

G_Allen wrote:that works for the 1st value of Field1
I then added
IniRead, Val, %INIFile%, %Field2%, Text_Body to get the Field2 value and get ERROR
That's not correct. You have "Text_Body" in there twice because Field2 contains "Text_Body". Replace %Field2% with %Subject%.
Guest

Re: help with IniRead

21 Sep 2017, 15:19

iN MY INIWrite,% "`tSubject`t`t= " Field1 "`n"
. "`tText_Body`t= " Field2 "`n"

,% INIFile,% Field1 ; using 'cosmetical' tabs for better readabilty, feel free to remove'm all :)

I get in the .ini file
[Gary Miller]
Subject = Gary Miller ;input from Field variable
Text_Body = Message 2 ;input from Field2 variable

IniRead, Value, %INIFile%, %Field1%, Subject gets me
Gary Miller Message 2
I am not able to get anthing but ERROR for the value of what was put into Field2.
I have tried
IniRead, Val, %INIFile%, %Subject%, Text_Body
IniRead, Val, %INIFile%, %Field2%, TextBody

What I need to get is: Gary Miller Message 2 which is the input to variables Field1 and Field2.
User avatar
boiler
Posts: 16977
Joined: 21 Dec 2014, 02:44

Re: help with IniRead

21 Sep 2017, 15:41

Does your script assign "Garry Miller" to the variable called Subject? Does the variable Field2 contain "Text_Body"?
Then IniRead, Val, %INIFile%, %Subject%, %Field2% should work.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: help with IniRead

21 Sep 2017, 17:59

Actually, I believe the second one should be IniRead, Val, %INIFILE%, %Field1%, Text_Body.

This is what I'm talking about with confusing variables. That third parameter represents what is in the [ ] in the Ini. In this case, the answer is Gary Miller -- which %Field1% holds it's value. When you used IniWrite, you used %Field1% for the name of the Section.

You have never created any %Subject% or %Body% or %Text_Body% variables.

Edit for a bit more clarity. Follow the colors from my previous reply.
Exaskryz wrote:
INIWrite,% "`tSubject`t`t= " Field1 "`n"
. "`tText_Body`t= " Field2 "`n"

,% INIFile,% Field1 ; using 'cosmetical' tabs for better readabilty, feel free to remove'm all :)
IniRead, Value, %INIFile%, %Field1%, Subject. Notice this is not %Subject%, but the literal string Subject.
That third parameter in IniRead is colored Orange. In the IniWrite, you have only a single Orange parameter, the last one, which was told to use the variable %Field1% (just using forced-expression mode). There is no other variable that would match up and be consistent. So in this isolated case, your %Field1% is "static" -- You use %Field1% to retrieve both values for the keys Subject and Text_Body.

There is a lot of confusion because you use %Field1% and %Field1% (again, in forced-expression mode) inside the IniWrite command. You are using the same variable in two different spots.

Also, emphasis was added to my original reply regarding the key Subject not being a variable.
Last edited by Exaskryz on 21 Sep 2017, 18:10, edited 3 times in total.
User avatar
boiler
Posts: 16977
Joined: 21 Dec 2014, 02:44

Re: help with IniRead

21 Sep 2017, 18:05

Yeah, I thougt I saw in one of the above scripts where it was assigned to Subject and another one where it's to one of the Field variables. You're right that well-named variables make all the difference.
Guest

Re: help with IniRead

21 Sep 2017, 19:19

So, in my IniWrite in the line

,% INIFile,% Field1 ; using 'cosmetical' tabs for better readabilty, feel free to remove'm all :)

I should be using something other than Field1 such as TxtMsg. Is that where I am going wrong?
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: help with IniRead

21 Sep 2017, 19:59

I would recommend that, yes. Whether you use ,% INIFile, TxtMsg or put in a field in your Gui Gui, Add, Edit, x+10 y36 w180 hp vTxtMsg , Section Name (with the position coordinates changed), and use ,% INIFile, % TxtMsg is up to you on that preference.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 306 guests