AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Using IniRead to read multiple keys

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
TheGood



Joined: 30 Jul 2007
Posts: 11

PostPosted: Mon Sep 01, 2008 2:26 am    Post subject: Using IniRead to read multiple keys Reply with quote

OK, so, I'm trying to read values from a section in an ini file which could have an arbitrary number of keys with arbitrary names. Is there a way to use the IniRead function without knowing the key name? My guess is no, since the Key parameter is not optional, but maybe there's wildcards or something allowed. Otherwise I guess I'll just have to parse the stuff out.
Back to top
View user's profile Send private message
poo_noo



Joined: 08 Dec 2006
Posts: 137
Location: Sydney Australia

PostPosted: Mon Sep 01, 2008 2:58 am    Post subject: Reply with quote

http://www.autohotkey.com/forum/viewtopic.php?t=24302
http://www.autohotkey.com/forum/viewtopic.php?t=21346

The above should help. I have apps that read keys with multiple sections.

Code:
;############# Read variables from INI file         ################################################

; adapted from http://www.autohotkey.com/forum/viewtopic.php?t=21346
; adapted from http://www.autohotkey.com/forum/viewtopic.php?t=24302
; set the INI location
; read the INI file

IfNotExist,%sharedfolder%\ahk_config.ini
   local_inifile = %A_MyDocuments%\AutoHotkey\ahk_config.ini
Else
   local_inifile = %sharedfolder%\ahk_config.ini
;local_inifile = %A_MyDocuments%\AutoHotkey\ahk_config.ini
initializeini(){
   global
   local key
   inisections:=0

   loop,read,%local_inifile%
{
   if regexmatch(A_Loopreadline,"\[(\w+)]")
   {
      inisections+= 1
      section%inisections%:=regexreplace(A_loopreadline,"(\[)(\w+)(])","$2")
      section%inisections%_keys:=0
   }
   else if regexmatch(A_LoopReadLine,"(\w+)=(.*)")
   {
      section%inisections%_keys+= 1
      key:=section%inisections%_keys
      section%inisections%_key%key%:=regexreplace(A_LoopReadLine,"(\w+)=(.*)","$1")
   }
   }
}

loadini(){
   global
   local sec,var
   loop,%inisections%
{
      sec:=A_index
      loop,% section%a_index%_keys
   {
      var:=section%sec%_key%A_index%
      iniread,%var%,%local_inifile%,% section%sec%,% section%sec%_key%A_index%
   }
   }
}

; read the variables from the ahk_config.ini file by calling the following functions
initializeini()
loadini()


Ini file looks like this
Code:
[site_details]
mybap=server02
myPrinter=server102\LPSiips
mybackupServer=server201\AHK\

_________________
Paul O
Back to top
View user's profile Send private message Visit poster's website
TheGood



Joined: 30 Jul 2007
Posts: 11

PostPosted: Mon Sep 01, 2008 3:08 am    Post subject: Reply with quote

Thanks!
Although I already wrote a quick and dirty script to extract what I want.
But your way is much cleaner!
Back to top
View user's profile Send private message
poo_noo



Joined: 08 Dec 2006
Posts: 137
Location: Sydney Australia

PostPosted: Mon Sep 01, 2008 3:18 am    Post subject: Reply with quote

Thanks, I wish I could take credit but it is my adaptation of the work of others with help from the forum.

http://www.autohotkey.com/forum/viewtopic.php?t=24302
http://www.autohotkey.com/forum/viewtopic.php?t=21346

Glad it works for you too.
_________________
Paul O
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group