Storing data - INI

INI

An INI file is a in section seperated configuration file which allows you to store different data in one or more sections.

The structure of an ini file is simple:

1 [StructureName]
2 Key1=Name
3 Key2=Age
4 Key3=EyeColour
5 [StructureName2]
6 Key1=Name
7 Key2=Age
8 Key3=EyeColour

It’s perfect for storing numbers or important information which the program or the script needs. It also provides a good overview.

IniWrite

As the name already says, you can use this command to write values into an ini. The command’s structure is:

IniWrite, Value, Filename, Section, Key

The value can be either a number or letters. Please note, you can not keep this emty - instead use %A_Space%, in case you don’t want to add something.

Secondly, you need to add the filepath of the ini file, so the script can add the value to it. It can be either normal string or a variable like %A_desktop%\example.ini. The section, as above already mentioned, is basically the data block’s name. The script needs it to work out which Key (see example) it should use because the ini file allows you to use duplicated key names. And finally, the key, you add it to retrieve the piece of data which you previously added to it.

An example:

1 ; any AutoHotkey version
2 IniWrite,Tim,%A_desktop%\example.ini,Member1,Key1

The result in the ini would look like:

1 [Member1]
2 Key1=Tim

IniRead

Imagine, you would have a script which allows the user to add details of a person to an ini. The IniWrite was a good example for it. But how would you get these details to show up without opening the ini file?

The IniRead command allows you to access it - and it isn’t even complicated! The command’s structure is basically as the IniWrite one with 2 differences: the outputvar (and the default - later)

IniRead, OutputVar, Filename, Section, Key [, Default]

As always, it starts with the command itself, followed by the variable where to store the ini’s key’s data (e.g the name of member1). Then comes the file name, where it will access the ini. Followed by the section and the key.

Let’s take a look on the iniwrite example again:

1 [Member1]
2 Key1=Tim

How would you retrieve the string Tim? Already figured it out? Good.

1 ; any AutoHotkey version
2 IniRead,Name,%A_desktop%\example.ini,Member1,Key1
3 MsgBox % Name ; Will show you the name you retrieved - which will be: Tim

Or you retrieve the data of the key1 from the second member, then it could be:

1 ; any AutoHotkey version
2 IniRead,Name,%A_desktop%\example.ini,Member2,Key1

Same key name but a different block name.

IniDelete

So now, the member left your community but the details about him are still in the ini file - how would you delete them? - manually? We’ll, of course, use a command again. This time it’s IniDelete.

The command’s structure:

IniDelete, Filename, Section [, Key]

As you already saw, it contains this []. You’ve learned before that these indicate that the command part enclosed by them is optional.

Back to our example:

1 [Member1]
2 Key1=Tim

We now want to delete his name:

1 ; any AutoHotkey version
2 IniDelete,%A_desktop%\example.ini,Member1,Key1

This will only delete Key1 of the section ‘Member1’. To delete the whole section, we simply remove the key name:

1 ; any AutoHotkey version
2 IniDelete,%A_desktop%\example.ini,Member1

Full example script:

 1 ; any AutoHotkey version
 2 ; You can even use the output of a msgbox or a control to write it into the ini.
 3 ; Imagine an user inputed the age of Tim in an InPutBox, we'll now use it in the command.
 4 ; AgeOfTim (the variable we use for the output contains 21
 5 IniWrite,Tim,%A_desktop%\example.ini,Member1,Name
 6 IniWrite,%AgeOfTim%,%A_desktop%\example.ini,Member1,Age
 7 IniWrite,blue,%A_desktop%\example.ini,Member1,EyeColour
 8 
 9 IniWrite,Tom,%A_desktop%\example.ini,Member1,Name
10 IniWrite,Age,%A_desktop%\example.ini,Member1,Age
11 IniWrite,green,%A_desktop%\example.ini,Member1,EyeColour
12 
13 ; We'll now read the ini's data 
14 IniRead,Name,%A_desktop%\example.ini,Member1,Name ; You can use the same name as the output & key name
15 ; But make sure, you use another name for the second user if you are retrieving the information at once without using them right after (example afer this one)
16 IniRead,Age,%A_desktop%\example.ini,Member1,Age
17 IniRead,EyeColour,%A_desktop%\example.ini,Member1,EyeColour
18 
19 IniRead,Name2,%A_desktop%\example.ini,Member2,Name
20 IniRead,Age2,%A_desktop%\example.ini,Member2,Age
21 IniRead,EyeColour2,%A_desktop%\example.ini,Member2,EyeColour
22 
23 MsgBox % "Member1: " Name
24              . "His age: " Age
25              . "His eye colour: " eyecolour
26              . "Member2: " Name2
27              . "His age: " Age2
28              . "His eye colour: " eyecolour2
29 IniDelete,%A_desktop%\example.ini,Member1
30 IniDelete,%A_desktop%\exmaple.ini,Member2 ; ini is now emty.

In one of the comments I said, it wouldn’t be smart to use the same variable to retrieve a content of 2 different blocks/sections. Let’s say, the script saves Tim in the variable Name but then it goes to the second section and saves Tom in the same variable - what do you think does the variable Name now contain? Right, it contains Tom because it automatically overwrote the first content.

Tip

If you do not want to store the information in a seperate INI file you can use the script file itself by adding a commented section with the proper INI sections and keys. Use the built-in variable A_ScriptFullPath. Of course this will only work for uncompiled scripts.

example

 1 ; any AutoHotkey version
 2 ; your script
 3 
 4 ; to read:
 5 IniRead, Key1, %A_ScriptFullPath%, settings, key1
 6 
 7 ; to write:
 8 IniWrite, %key1%, %A_ScriptFullPath%, settings, key1
 9 
10 /*
11 [settings]
12 key1=value
13 key2=value
14 
15 */