igorm Posted March 17, 2008 Share Posted March 17, 2008 Hi, I need a help with searching .ini file. I need to search for specific key value and as return I need name of the section where value is located. Thanks in advance for any help. Cheers Office 2000/XP/2003/2007 Slipstreamer Link to comment Share on other sites More sharing options...
Bert Posted March 17, 2008 Share Posted March 17, 2008 StringInStr The Vollatran project My blog: http://www.vollysinterestingshit.com/ Link to comment Share on other sites More sharing options...
weaponx Posted March 17, 2008 Share Posted March 17, 2008 expandcollapse popup$result = SearchINI("test.ini", "PASSWORD", true, 2, 0) MsgBox(0,"",$result) ;FileName = Path to ini file ;ExactMatch = true - match exact string, false - match partial ;Type = Type of search: 0 = Keys and Values, 1 = Keys only, 2 = Values only (default) ;Return = Return type: 0 = String containing section name (default), 1 = true/false Func SearchINI($sFileName, $sSearchString, $fExactMatch = false, $iType = 2, $iReturn = 0) $sMatchingSection = "" ;Retrieve section names $aSectionNames = IniReadSectionNames($sFileName) ;Loop through all sections For $X = 1 to $aSectionNames[0] ;Retrieve section data $aSectionData = IniReadSection ($sFileName, $aSectionNames[$X]) ;Loop through all keys/values in section For $Y = 1 to $aSectionData[0][0] ;Search keys only If $iType <> 2 Then If $fExactMatch Then If $aSectionData[$Y][0] = $sSearchString Then $sMatchingSection = $aSectionNames[$X] Else If StringInStr($aSectionData[$Y][0], $sSearchString) Then $sMatchingSection = $aSectionNames[$X] EndIf EndIf ;Search values only If $iType <> 1 Then If $fExactMatch Then If $aSectionData[$Y][1] = $sSearchString Then $sMatchingSection = $aSectionNames[$X] Else If StringInStr($aSectionData[$Y][1], $sSearchString) Then $sMatchingSection = $aSectionNames[$X] EndIf EndIf Next Next ;Return specified type If $iReturn = 0 Then Return $sMatchingSection Else If $sMatchingSection <> "" Then Return True Else Return False EndIf EndIf EndFunc Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted March 17, 2008 Moderators Share Posted March 17, 2008 (edited) I like weaponx's, this could also be modded to return "all" sections if someone put forth a little effort:MsgBox(64, "Info", _StringGetIniSection(@DesktopDir & "\myTest.ini", "8", 1)) Func _StringGetIniSection($hIni, $sFind, $vExact = ".*?") If $vExact Then $vExact = "" Local $aSRE = StringRegExp(@LF & FileRead($hIni), _ "(?s)(?i)\n\s*\[\s*(.*?)\s*\]\s*\r\n.*?=" & $vExact & $sFind & $vExact & "(?m:$|\[|\r\n)", 3) If IsArray($aSRE) = 0 Then Return SetError(1, 0, "") Return $aSRE[0] EndFuncoÝ÷ ØGb´-¹©eȬ¶·º¹âayªëk$^¶êçÓ~I,¥v·º¹ÚZrêëzw±*"Ë^iÚ¶nÞʦm)¶hjëh×6Func _StringGetIniSection($hIni, $sFind, $vExact = ".*?", $nReturnAll = 0) If $vExact Then $vExact = "" Local $aSRE = StringRegExp(@LF & FileRead($hIni), _ "(?s)(?i)\n\s*\[\s*(.*?)\s*\]\s*\r\n.*?=" & $vExact & $sFind & $vExact & "(?m:$|\[|\r\n)", 3) If IsArray($aSRE) = 0 Then Return SetError(1, 0, "") If $nReturnAll Then Return $aSRE Return $aSRE[0] EndFunc Edited March 17, 2008 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
igorm Posted March 17, 2008 Author Share Posted March 17, 2008 I can just say: wow.Thank you guys very much. Special thanks to weaponx. This function is really excellent. I like it a lot.Cheers Office 2000/XP/2003/2007 Slipstreamer Link to comment Share on other sites More sharing options...
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