IniRead

Reads a value, section or list of section names from a standard format .ini file.

IniRead, OutputVar, Filename, Section, Key , Default
IniRead, OutputVarSection, Filename, Section
IniRead, OutputVarSectionNames, Filename

Parameters

OutputVar

The name of the output variable in which to store the retrieved value. If the value cannot be retrieved, the variable is set to the value indicated by the Default parameter (described below).

OutputVarSection

[AHK_L 57+]: Omit the Key parameter to read an entire section. Comments and empty lines are omitted. Only the first 65,533 characters of the section are retrieved.

OutputVarSectionNames

[AHK_L 57+]: Omit the Key and Section parameters to retrieve a linefeed (`n) delimited list of section names.

Filename

The name of the .ini file, which is assumed to be in %A_WorkingDir% if an absolute path isn't specified.

Section

The section name in the .ini file, which is the heading phrase that appears in square brackets (do not include the brackets in this parameter).

Key

The key name in the .ini file.

Default

If blank or omitted, it defaults to the word ERROR. Otherwise, specify the value to store in OutputVar if the requested key is not found. To store a blank value (empty string), specify %A_Space%.

[AHK_L 57+]: This parameter is not used if Key is omitted.

Error Handling

ErrorLevel is not set by this command. If there was a problem, OutputVar will be set to the default value as described above.

Remarks

The operating system automatically omits leading and trailing spaces/tabs from the retrieved string. To prevent this, enclose the string in single or double quote marks. The outermost set of single or double quote marks is also omitted, but any spaces inside the quote marks are preserved.

Values longer than 65,535 characters are likely to yield inconsistent results.

A standard ini file looks like:

[SectionName]
Key=Value

Unicode: IniRead and IniWrite rely on the external functions GetPrivateProfileString and WritePrivateProfileString to read and write values. These functions support Unicode only in UTF-16 files; all other files are assumed to use the system's default ANSI code page.

IniDelete, IniWrite, RegRead, file-reading loop, FileRead

Examples

Reads the value of a key located in section2 from a standard format .ini file and stores it in OutputVar.

IniRead, OutputVar, C:\Temp\myfile.ini, section2, key
MsgBox, The value is %OutputVar%.