INI files and reading them

Discuss RoboTask here
Post Reply
photoevents
Posts: 44
Joined: Mon Jan 01, 2024 11:21 am
Contact:

INI files and reading them

Post by photoevents »

I use an INI file with section settings and about 20 values
To read all those values in the variables I use INI read twenty times and it works.
I cannot figure out how to use the INI loop to do the same thing.
Is it possible?

Christian
Oleg
Site Admin
Posts: 3084
Joined: Thu Jan 01, 1970 1:00 am
Contact:

Re: INI files and reading them

Post by Oleg »

Look at my examples below.

This example reads INI section in the loop into variable using JSON format

Code: Select all

;*****************************
;* RoboTask Task file
;* Do not edit in text editor!
;*****************************
 
[Root]
ActionAfterRun=INTEGER|0
Actions=FOLDER
Automat=INTEGER|-1
CatID=INTEGER|444033236
Comment=STRINGLIST
ContinueOnError=INTEGER|0
DisableOnError=INTEGER|0
DoNotStopWhenShutdown=INTEGER|0
ExternalName=STRING|"Task1515"
Hide=INTEGER|0
ID=INTEGER|-77708332
LogOnAsUser=INTEGER|1
Name=STRING|"Read section in the loop (JSON)"
OnErrorTaskID=INTEGER|0
Priority=INTEGER|3
RunOnClose=INTEGER|0
RunOnStartup=INTEGER|0
StepPause=INTEGER|0
ToLog=INTEGER|3
UnicodeFormat=INTEGER|1
WriteGeneralLog=INTEGER|0

[Actions]
Action1=FOLDER
Action2=FOLDER
Action3=FOLDER
Action4=FOLDER
Action5=FOLDER
Action6=FOLDER
Action7=FOLDER

[Actions\Action1]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""IniFile"" with value ""C:\temp\test.ini"""
Params=FOLDER

[Actions\Action1\Params]
_rt_variables_produced=STRING|"IniFile"
expand=STRING|"0"
linecount=STRING|"1"
varname=STRING|"IniFile"
varvalue=STRING|"C:\temp\test.ini"

[Actions\Action2]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""IniSection"" with value ""root"""
Params=FOLDER

[Actions\Action2\Params]
_rt_variables_produced=STRING|"IniSection"
expand=STRING|"0"
linecount=STRING|"1"
varname=STRING|"IniSection"
varvalue=STRING|"root"

[Actions\Action3]
ActionID=STRING|"A_INI_LOOP"
Enabled=INTEGER|-1
Name=STRING|"INI Loop (by keys)"
Params=FOLDER

[Actions\Action3\Params]
_rt_variables_produced=STRING|"key,val"
filename=STRING|"{IniFile}"
keynamevar=STRING|"key"
keyvaluevar=STRING|"val"
loopmode=STRING|"1"
section=STRING|"{IniSection}"

[Actions\Action4]
ActionID=STRING|"A_ROBOTASK_LOG"
Enabled=INTEGER|-1
Name=STRING|"Log Message"
Params=FOLDER

[Actions\Action4\Params]
message=STRING|"Key name = {key}; Value = {val}"
type=STRING|"3"

[Actions\Action5]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""json:Collection.{key}"" with value ""{val}"""
Params=FOLDER

[Actions\Action5\Params]
_rt_variables_produced=STRING|"json:Collection.{key}"
expand=STRING|"1"
linecount=STRING|"1"
varname=STRING|"json:Collection.{key}"
varvalue=STRING|"{val}"

[Actions\Action6]
ActionID=STRING|"A_LOOP_END"
Enabled=INTEGER|-1
Name=STRING|"End Loop"

[Actions\Action7]
ActionID=STRING|"A_DIALOG_SHOWTEXT"
Enabled=INTEGER|-1
Name=STRING|"Show Text"
Params=FOLDER

[Actions\Action7\Params]
autoclose=STRING|"0"
fromfile=STRING|"1"
line00000000=STRING|"JSON collection completely:"
line00000001=STRING|"{Collection}"
line00000003=STRING|"*************"
line00000004=STRING|"How to read each value use {json:collection.<key_name>}"
line00000005=STRING|"Note that JSON is case sensitive"
line00000006=STRING|"for example"
line00000007=STRING|"{json:collection.Priority}"
line00000008=STRING|"{json:collection.ID}"
line00000009=STRING|"{json:collection.Name}"
linecount=STRING|"10"
timeout=STRING|"10"

This example reads INI section into separate variables.
But note that KeyName may contain an unacceptable value for the variable name. The INI file allows almost any value for the key name.
In this case the variable will not be initialized and you will see a warning in the task log

Code: Select all

;*****************************
;* RoboTask Task file
;* Do not edit in text editor!
;*****************************
 
[Root]
ActionAfterRun=INTEGER|0
Actions=FOLDER
Automat=INTEGER|-1
CatID=INTEGER|444033236
Comment=STRINGLIST
ContinueOnError=INTEGER|0
DisableOnError=INTEGER|0
DoNotStopWhenShutdown=INTEGER|0
ExternalName=STRING|"Task1516"
Hide=INTEGER|0
ID=INTEGER|2023322647
LogOnAsUser=INTEGER|1
Name=STRING|"Read section in the loop (separate variables)"
OnErrorTaskID=INTEGER|0
Priority=INTEGER|3
RunOnClose=INTEGER|0
RunOnStartup=INTEGER|0
StepPause=INTEGER|0
ToLog=INTEGER|3
UnicodeFormat=INTEGER|1
WriteGeneralLog=INTEGER|0

[Actions]
Action1=FOLDER
Action2=FOLDER
Action3=FOLDER
Action4=FOLDER
Action5=FOLDER
Action6=FOLDER
Action7=FOLDER

[Actions\Action1]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""IniFile"" with value ""C:\temp\test.ini"""
Params=FOLDER

[Actions\Action1\Params]
_rt_variables_produced=STRING|"IniFile"
expand=STRING|"0"
linecount=STRING|"1"
varname=STRING|"IniFile"
varvalue=STRING|"C:\temp\test.ini"

[Actions\Action2]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""IniSection"" with value ""root"""
Params=FOLDER

[Actions\Action2\Params]
_rt_variables_produced=STRING|"IniSection"
expand=STRING|"0"
linecount=STRING|"1"
varname=STRING|"IniSection"
varvalue=STRING|"root"

[Actions\Action3]
ActionID=STRING|"A_INI_LOOP"
Enabled=INTEGER|-1
Name=STRING|"INI Loop (by keys)"
Params=FOLDER

[Actions\Action3\Params]
_rt_variables_produced=STRING|"key,val"
filename=STRING|"{IniFile}"
keynamevar=STRING|"key"
keyvaluevar=STRING|"val"
loopmode=STRING|"1"
section=STRING|"{IniSection}"

[Actions\Action4]
ActionID=STRING|"A_ROBOTASK_LOG"
Enabled=INTEGER|-1
Name=STRING|"Log Message"
Params=FOLDER

[Actions\Action4\Params]
message=STRING|"Key name = {key}; Value = {val}"
type=STRING|"3"

[Actions\Action5]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""{key}"" with value ""{val}"""
Params=FOLDER

[Actions\Action5\Params]
_rt_variables_produced=STRING|"{key}"
expand=STRING|"1"
linecount=STRING|"1"
varname=STRING|"{key}"
varvalue=STRING|"{val}"

[Actions\Action6]
ActionID=STRING|"A_LOOP_END"
Enabled=INTEGER|-1
Name=STRING|"End Loop"

[Actions\Action7]
ActionID=STRING|"A_DIALOG_SHOWTEXT"
Enabled=INTEGER|-1
Name=STRING|"Show Text"
Params=FOLDER

[Actions\Action7\Params]
autoclose=STRING|"0"
fromfile=STRING|"1"
line00000000=STRING|"Priority = {Priority}"
line00000001=STRING|"id = {ID}"
line00000002=STRING|"Name = {Name}"
linecount=STRING|"3"
timeout=STRING|"10"
Oleg Yershov
photoevents
Posts: 44
Joined: Mon Jan 01, 2024 11:21 am
Contact:

Re: INI files and reading them

Post by photoevents »

Thanks, it's kind of obvious but I'm still learning.

Christian
Post Reply