Sign in to follow this  
Followers 0
Matt @ MPCS

Enumerate INI Sections

10 posts in this topic

#1 ·  Posted (edited)

I converted an old VB application to AutoIt and required this function. I found it useful, hopefully someone else will too.

Func INISectionEnum( $IniFile )
  
   Local $i, $tmpLine, $file, $strSections
  
   If( Not FileExists( $IniFile ) ) Then
      SetError( -1 )
      Return 0
   EndIf
  
   $file = FileOpen( $IniFile, 0 )
  
   If( $file = -1 ) then 
      SetError( -2 )
      Return 0
   EndIf
  
   While 1
      $tmpLine = FileReadLine( $file )
        
      If( @error = -1 ) Then
         ExitLoop
      EndIf
        
      If( StringLeft( $tmpLine, 1 ) = "[" ) Then
           
         $i = StringInStr( $tmpLine, "]" )
           
         If( $i = 0 ) Then
            ContinueLoop
         EndIf
           
         If( $strSections = "" ) Then
            $strSections = StringMid( $tmpLine, 2, ($i - 2) ) 
         Else
            $strSections = $strSections & "|" & StringMid( $tmpLine, 2, ($i - 2) ) 
         EndIf
        
      EndIf
     
   Wend

   If( $strSections = "" ) Then
      SetError( 1 )
      Return 0
   EndIf
  
  Return StringSplit( $strSections, "|" )

EndFunc

Func INIKeyEnum( $IniFile, $Section )
   
   Local $file, $tmpLine, $numLine, $strKeys
   
   If( Not FileExists( $IniFile ) ) Then
      SetError( -1 )
      Return 0
   EndIf

   $file = FileOpen( $IniFile, 0 )
   
   If( $file = -1 ) then 
      SetError( -2 )
      Return 0
   EndIf
   
   $numLine = 0
   
   While 1
      
      $numLine = $numLine + 1
      
      $tmpLine = FileReadLine( $file )
      
      If( @error = -1 ) Then
         ExitLoop
      EndIf
        
      If( StringLeft( $tmpLine, (StringLen( $Section ) + 2) ) = "[" & $Section & "]" ) Then
           
         $i = $numLine + 1
         
         While 1
            
            $tmpLine = FileReadLine( $file, $i )
            
            If( (@error = -1) Or (StringLeft( $tmpLine, 1 ) = "[") ) Then
               ExitLoop
            EndIf

            If( StringStripWS( $tmpLine, 8 ) = "" ) Then ContinueLoop
            
            If( $strKeys = "" ) Then
               $strKeys = StringLeft( $tmpLine, StringInStr( $tmpLine, "=" ) - 1 )
            Else
               $strKeys = $strKeys & "|" & StringLeft( $tmpLine, StringInStr( $tmpLine, "=" ) - 1 )
            EndIf
           
            $i = $i + 1

         Wend

         Return StringSplit( $strKeys, "|" )
         
      EndIf
   
   Wend
   
EndFunc

It isn't rocket science but hopefully somone will find it useful. Thanks guys!

*** Matt @ MPCS

EDIT: Standardized the code.

EDIT2: Updated code to add new function.

Edited by Matt @ MPCS

Share this post


Link to post
Share on other sites



I don't see why that should cause a problem...

Share this post


Link to post
Share on other sites

Yes. It is a valid way to do things.


Who else would I be?

Share this post


Link to post
Share on other sites

I got used to that while writing C++ code; if it bothers you and you want to use the code it won't hurt to remove the parents. I think the only statement that it doesn't wor on is the for statement.

*** Matt @ MPCS

Share this post


Link to post
Share on other sites

In addition to the ability to enumerate the sections, I have now written a function that will enumerate the keys of an INI file. Refer to the code in the first post for this update. Thanks!

*** Matt @ MPCS

Share this post


Link to post
Share on other sites

@bcording

I didn't see it posted, so I wrote my own. It would have been nice to have instead of doing it myself. It is amazing the similarities between them. You do a few conditionals I don't think should be/need to be done, but if it works then it works.

FYI: Technically the official name for your 'SectionEntries' is 'Keys'. This was defined by Microsoft and is the industry standard.

*** Matt @ MPCS

Share this post


Link to post
Share on other sites

FYI: Technically the official name for your 'SectionEntries' is 'Keys'. This was defined by Microsoft and is the industry standard.

<{POST_SNAPBACK}>

And we all care about Micro$not and the industry standard :)

CoProc Multi Process Helper libraryTrashBin.nfshost.com store your AutoIt related files here!AutoIt User Map

Share this post


Link to post
Share on other sites

A section entry is a key.

A key is a section entry.

Yes, sounds good to me. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0