Jump to content

Searh in a .ini


Clean
 Share

Recommended Posts

Hey,

I apologize in adavance for my poor english

Anyway im new on the forum, but i'm coding in Autoit since a bit of time, i needed a searching fuction in a .ini file and didn't find anything.

So i've coded this, i know this is a non optimized algorithme etc, but it can help some people someday.

; Search in a .ini file a specific string and return an array of section/key couple
; the [0][0] result's index is egal to the number of results
Func IniSearch($path,$value)
    Local $SectionNames = IniReadSectionNames($path)
    Local $Section, $Result
    Local $Return[1][2]
    $Return[0][0] = 0
    For $i = 1 To $SectionNames[0]
        $Section = IniReadSection($path,$SectionNames[$i])
        $Result = _ArraySearch($Section,$value,0,UBound($Section)-1,0,0,1,2)
        If $Result <> -1 Then
            ReDim $Return[UBound($Return)+1][2]
            $Return[0][0] += 1
            $Return[UBound($Return)-1][0] = $SectionNames[$i]
            $Return[UBound($Return)-1][1] = $Section[$Result][0]
        EndIf
    Next
    Return $Return
EndFunc

i'm sorry for posting it with my poor programming level, but this is this kind of things who makes you cry when you don't find how to do it

Clean

Edited by Clean
Link to comment
Share on other sites

Hi Clean, welcome to the AutoIt forum

We all must start somewhere and thankyou for your contribution to the community.

However I believe you have unintentionally posted your work in the wrong forum.

I recommend posting here:

http://www.autoitscript.com/forum/forum/9-example-scripts/

or respectfully request a moderator move this thread there. :)

Edited by Mobius

wtfpl-badge-1.png

Link to comment
Share on other sites

with no udfs

$aTEST = _INI_Value_Search("desktop.ini" , "TESTVALUE1")

msgbox(0, '' , "Section: " & $aTest[0][0] & @CRLF & "Key: " & $aTest[0][1])

Func _INI_Value_Search($path , $value)
Local $aFile['']
Local $aOut[1][2]

$sFile = FileRead($path)
$aFile = StringSplit($sFile , @LF , 2)

For $i = ubound($aFile) - 1 to 0 step -1

    If stringinstr($aFile[$i] , "=") = 0 Then ContinueLoop
    If StringStripWS(stringsplit($aFile[$i] , "=" , 2)[1] , 8) = $value Then

        $aOut[0][1] = stringsplit($aFile[$i] , "=" , 2)[0]
            For $k = $i to 0 step - 1
                If stringleft($aFile[$k] , 1) = "[" then
                    $aOut[0][0] = stringtrimleft(stringtrimright(stringstripWS($aFile[$k] , 8) , 1) , 1)
                    return $aOut
                EndIf
            Next
    Endif
Next

EndFunc

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Yes,

But many things are optimizable in this code.

For exemple it doesn't support multi reports, list reports, etc

I gonna implement a list report because i need it.

Anyway the algorithm is "bad" (i.e. non optimized), when searching in big file it gonna take a looooonnng time i think :o

feel free to help/correct/give a better version/comment/etc :P

Edited by Clean
Link to comment
Share on other sites

You use _ArraySearch, which uses the Array.au3 UDF.

boththose's script uses only native AutoIt functionality.

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

yeah was thinking about the _arraysearch.

anyway i dont like using this kind of functions, using it for fast coding - i'll recode it :P
and thanks boththose for your contribution

Edited by Clean
Link to comment
Share on other sites

Sure, you will have to adjust on mine a little If your values are not unique, and you want every key and section in which that value appears.  With #5 you will only get the last instance.

And there is no problem with UDFs, the time and size hits are generally negligible, that was merely for sport.  More showing that there are many ways to accomplish this when you just treat it like any other file with delimiters consisting of equal signs and brackets.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Plenty of UDFs sit atop the shoulders of other UDFs. Like boththose said, 

More showing that there are many ways to accomplish this 

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

More showing that there are many ways to accomplish this when you just treat it like any other file with delimiters consisting of equal signs and brackets.

 

Yep .ini files are just .txt file with a specific "architecture"

Link to comment
Share on other sites

with no udfs

Local $aFile['']

This isn't correct.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

pretty simple but with a lists (or not), replace _ArraySearch with ArraySearchlist :

; list = "a,b,c,d,..."
Func ListSearch($string,$value)
    Local $List = StringSplit($string,",")
    If $List[0] == 0 Then Return False
    For $i = 1 To $List[0]
        If $List[$i] == $value Then Return True
    Next
    Return False
EndFunc

Func ArraySearchList($array,$value,$start=1,$stop=UBound($array)-1)
    For $i = $start To $stop
        If ListSearch($array[$i][1],$value) Then Return $i
    Next
    Return -1
EndFunc
Edited by Clean
Link to comment
Share on other sites

really?  Is 0 the preferred way to declare a blank array?

#include <array.au3>

Local $aFile['']
_ArrayDisplay($aFile)

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Clean,

Sorry to be picky, but there is no need for Local $String. I am also not confident with that UBound() in the parameter list, perhaps this is a job for the default keyword.
 

#include <Constants.au3>

Func ArraySearchList($aArray, $vValue, $iStart = Default, $iEnd = Default)
    Local $iLength = UBound($aArray)
    If $iLength > 0 And UBound($aArray, $UBOUND_COLUMNS) >= 2 Then ; If the array has elements and at least 2 columns
        If $iStart = Default Then $iStart = 0 ; If the default keyword then set to zero
        If $iEnd = Default Then $iEnd = $iLength - 1 ; If the default keyword then set to the length - 1
        For $i = $iStart To $iEnd ; Loop
            If ListSearch($aArray[$i][1], $vValue) Then Return $i
        Next
    EndIf
    Return -1
EndFunc   ;==>ArraySearchList

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

 

really?  Is 0 the preferred way to declare a blank array?

#include <array.au3>

Local $aFile['']
_ArrayDisplay($aFile)

 

i don't know the autoit's arcanes, but an int value should be better managed than a string (autoconverted values)

Link to comment
Share on other sites

 

really?  Is 0 the preferred way to declare a blank array?

#include <array.au3>

Local $aFile['']
_ArrayDisplay($aFile)

An array should be declared with a integer, this is standard practice in all languages I know. What happens here is AutoIt is too kind that it translates "" to zero for you, but this is really bad practice to rely on the whole "" = 0 caveat. So a zero dimension array is Local $aArray[0]. Though in your example it's not needed as you can simple declare and intialise with the StringSplit return array instead i.e. less lines.

Edited by guinness

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...