Iniwrite sends new entries to the end of the script. Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
letacio
Posts: 11
Joined: 08 Mar 2018, 16:05

Iniwrite sends new entries to the end of the script.

08 Mar 2018, 16:56

Hello everybody!

I have this issue:

I use a ini section inside my script (didnt want to make a a separate file for this).

When i already have a key inside it, all goes right. I can read and overwrite it at will.

But i want to be able to add more entries during execution time (a user types something, and it's added to a section inside the script).
The problem is: when the key is not already present in the ini section, i use iniwrite, and the entries are inserted at the end of the script (sometimes not at the very end, i don't understand this behaviour, actually).

example: how my code is written before i execute script:

Code: Select all

/*
[inisection]
key1=  black
key2= blue
key3 = green.
*/

::write::

newvalue= red                 ; (this would be set using some input box or GUI, but im setting here for testing purposes
newkey= key4
IniWrite, %newkey%, %A_ScriptFullPath%, inisection, %newvalue%
return

how i want my code to be, after i execute the script:

Code: Select all

/*
[inisection]
key1=  black
key2= blue
key3 = green.
key4 = red     ; <----------------------------------------------- new entry should have been added here
*/

::write::
newvalue= red                                              
newkey= key4
IniWrite, %newkey%, %A_ScriptFullPath%, inisection, %newvalue%
return
how my code actually shows after running the script

Code: Select all

/*
[inisection]
key1=  black
key2= blue
key3 = green.
*/

::write::
newvalue= red                                             
newkey= key4
IniWrite, %newkey%, %A_ScriptFullPath%, inisection, %newvalue%
return

[inisection]       ;   <- --------------- *this line gives error : "this line does not contain recognized action"
key4=red       ;<------------------------------------------ the entry was sent here instead.
Plus, when i try to reload the script, it says that the line with " [inisection] " does not contain recognized action.

What am i doing wrong?

Thanks!
User avatar
gregster
Posts: 1494
Joined: 30 Sep 2013, 06:48

Re: Iniwrite sends new entries to the end of the script.

08 Mar 2018, 19:37

Interesting idea, but I don't think that the concept/standard of ini-file operations is meant like this, afaik AHK is relying on the standard windows API functions.
Treating an ahk-script as an ini-file is just not supported. An external/conventional ini-file is the right way to go. Using a standard ini-file, you won't have to worry about the position of entries, because it just won't be interpreted as an AHK script file at the same time.
That said, it might be possible to treat the file with the normal file operation commands/functions (as a text file) and place the entries at the "right" position, but I wouldn't recommend the effort.
BoBo
Posts: 2515
Joined: 13 May 2014, 17:15

Re: Iniwrite sends new entries to the end of the script.

09 Mar 2018, 01:10

Interesting. I've used/invented that "concept" about 12yrs (?) ago but only for already existing variables/keys. :shifty: :shh: :silent:
To prevent that errormsg from AHK's interpreter I've defined that INIfile section as a comment section (like said above).
Some AHK guru in here has advised me a few month ago that instead of this, one should use :arrow: Static variables bc that's the way to go while the script is running!
letacio
Posts: 11
Joined: 08 Mar 2018, 16:05

Re: Iniwrite sends new entries to the end of the script.

21 Mar 2018, 16:30

BoBo wrote:Interesting. I've used/invented that "concept" about 12yrs (?) ago but only for already existing variables/keys. :shifty: :shh: :silent:
To prevent that errormsg from AHK's interpreter I've defined that INIfile section as a comment section (like said above).
Some AHK guru in here has advised me a few month ago that instead of this, one should use :arrow: Static variables bc that's the way to go while the script is running!

It was awsome "invention" of yours by the way. :bravo:
I've been using this for some time and it works wonders. But I now want some more sophisticated functions that include creating new lines on my ini file during execution... anyway, i might use a separate file for this functions :think:

Thanks for the answers, both of you!
Odlanir
Posts: 274
Joined: 20 Oct 2016, 08:20

Re: Iniwrite sends new entries to the end of the script.

22 Mar 2018, 09:38

Stating that this is an "out of the box" method, the below code seems to works. You only have to put the ini code at the bottom of the script after a comment section.

Code: Select all

newvalue := "orange"
newkey   := "key4"
IniWrite, %newvalue%, %A_ScriptFullPath%, inisection, %newkey%

ExitApp

/*
[inisection]
key1=  black
key2= blue
key3=green
P.S: in your example you inverted the IniKey with the IniValue.
____________________________________________________________________________
Windows 7 Home Premium 64 bit - Autohotkey v1.1.24.02 64-bit Unicode
BoBo
Posts: 2515
Joined: 13 May 2014, 17:15

Re: Iniwrite sends new entries to the end of the script.  Topic is solved

22 Mar 2018, 09:51

Odlanir wrote:Stating that this is an "out of the box" method, the below code seems to works. You only have to put the ini code after a comment section.

Code: Select all

newvalue := "orange"
newkey   := "key4"
IniWrite, %newvalue%, %A_ScriptFullPath%, inisection, %newkey%

ExitApp

/*
[inisection]
key1=  black
key2= blue
key3=green
What happens once you're executing the script the next time? I'd expect that we'll see an error that the script contains illegal/unknown commands bc the comment-block hasn't been closed correctly. Correct me if I'm wrong.
Odlanir
Posts: 274
Joined: 20 Oct 2016, 08:20

Re: Iniwrite sends new entries to the end of the script.

22 Mar 2018, 09:59

@BoBo I've run it several times and I get any error. I'm used to end all my scripts with an openig comment (/*) an put after that all the code that can be useful for the script when needed. Never get any error at all. Try to run it and see.
Cheers.
____________________________________________________________________________
Windows 7 Home Premium 64 bit - Autohotkey v1.1.24.02 64-bit Unicode
User avatar
evilC
Posts: 4372
Joined: 27 Feb 2014, 12:30

Re: Iniwrite sends new entries to the end of the script.

22 Mar 2018, 10:14

The question begs though, WHY would you want to do that?
By using a separate INI file, you lose the ability to use the same settings with a different version of the script, without having to edit any code.
Also, it will not work with compiled scripts.
Finally, you slow down the IniRead / IniWrite code, as it has to parse through all the code of the script, to get to the INI settings.
Some AHK guru in here has advised me a few month ago that instead of this, one should use :arrow: Static variables bc that's the way to go while the script is running
Statics are for functions or classes. Declaring a global variable as static will have zero effect surely?
Odlanir
Posts: 274
Joined: 20 Oct 2016, 08:20

Re: Iniwrite sends new entries to the end of the script.

22 Mar 2018, 10:18

@evilC Totally agree with you. That's what I've tried to point out in my post. Even if I've gave the OP one way to accomplish his goal.
____________________________________________________________________________
Windows 7 Home Premium 64 bit - Autohotkey v1.1.24.02 64-bit Unicode
BoBo
Posts: 2515
Joined: 13 May 2014, 17:15

Re: Iniwrite sends new entries to the end of the script.

22 Mar 2018, 10:23

Odlanir wrote:@BoBo I've run it several times and I get any error. I'm used to end all my scripts with an openig comment (/*) an put after that all the code that can be useful for the script when needed. Never get any error at all. Try to run it and see.
Cheers.
That's interesting. Wasn't aware of that. Grazie :thumbup:
letacio
Posts: 11
Joined: 08 Mar 2018, 16:05

Re: Iniwrite sends new entries to the end of the script.

22 Mar 2018, 13:44

BoBo wrote:
Odlanir wrote:Stating that this is an "out of the box" method, the below code seems to works. You only have to put the ini code after a comment section.

Code: Select all

newvalue := "orange"
newkey   := "key4"
IniWrite, %newvalue%, %A_ScriptFullPath%, inisection, %newkey%

ExitApp

/*
[inisection]
key1=  black
key2= blue
key3=green
What happens once you're executing the script the next time? I'd expect that we'll see an error that the script contains illegal/unknown commands bc the comment-block hasn't been closed correctly. Correct me if I'm wrong.

Worked! So simple, thanks a lot.
Now i feel stupid for not thinking about it :lol:
lexikos
Posts: 6207
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Iniwrite sends new entries to the end of the script.

24 Mar 2018, 00:57

An INI section ends at the end of the file or before the start of the next section. If there is only one section, it would be perfectly correct for the entries to appear at the end of the file. So what if there's another section?

Code: Select all

/*
[inisection]
key1=  black
key2= blue
key3 = green.
[end]
*/
IniWrite, red, %A_ScriptFullPath%, inisection, key4
The Windows INI functions appear to preserve the section headers and content which isn't in INI format, treating such lines as comments. (Comments commonly appear in INF files, but I don't think any particular comment syntax is documented.) Since new key=value pairs must be placed inside the appropriate section, they will most likely be placed between key3 and [end].

When I tested (on Windows 10), key4 was always inserted immediately after the last "assignment" within inisection, which could be something like foo= placed further down in the script, as long as it preceded the next section header. foo= can be both a valid INI line and an actual assignment in AutoHotkey v1.

Return to “Ask For Help”

Who is online

Users browsing this forum: shipaddicted, Yurimi and 95 guests