Matt @ MPCS Posted October 12, 2004 Posted October 12, 2004 (edited) I converted an old VB application to AutoIt and required this function. I found it useful, hopefully someone else will too. expandcollapse popupFunc 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 October 12, 2004 by Matt @ MPCS
piccaso Posted October 12, 2004 Posted October 12, 2004 is it valid to wrap up the expression of a if statement?like that...If( $strSections = "" ) Then CoProc Multi Process Helper libraryTrashBin.nfshost.com store your AutoIt related files here!AutoIt User Map
Chris_1013 Posted October 12, 2004 Posted October 12, 2004 I don't see why that should cause a problem...
this-is-me Posted October 12, 2004 Posted October 12, 2004 Yes. It is a valid way to do things. Who else would I be?
Matt @ MPCS Posted October 12, 2004 Author Posted October 12, 2004 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
Matt @ MPCS Posted October 12, 2004 Author Posted October 12, 2004 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
bcording Posted October 18, 2004 Posted October 18, 2004 I thought this was posted already Get Ini Sections And Section Entries
Matt @ MPCS Posted October 18, 2004 Author Posted October 18, 2004 @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
piccaso Posted October 19, 2004 Posted October 19, 2004 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
MHz Posted October 20, 2004 Posted October 20, 2004 A section entry is a key. A key is a section entry. Yes, sounds good to me.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now