Sign in to follow this  
Followers 0
psylem

IniRead Orphaned Keys

5 posts in this topic

Ok, I've done some research and it's very not standard behaviour in an ini file. But what happens if you want to read some values from an ini file that has some stuff that is outside of any section?

I've tried...

IniRead("Test.INI","default", "screen_x", "0")

IniRead("Test.INI","", "screen_x", "0")

But no joy. Assuming I only have read access to this file, is there any way of getting the values using IniRead? Or do I have to code something to read the lines in the file?

Share this post


Link to post
Share on other sites



Ok, I've done some research and it's very not standard behaviour in an ini file. But what happens if you want to read some values from an ini file that has some stuff that is outside of any section?

I've tried...

IniRead("Test.INI","default", "screen_x", "0")

IniRead("Test.INI","", "screen_x", "0")

But no joy. Assuming I only have read access to this file, is there any way of getting the values using IniRead? Or do I have to code something to read the lines in the file?

Doubt if you can call something a INI file when it doesn't contain sections.

You will have to "FileRead" it yourself and scan for the right record.


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

psylem

Read in the help file, the structure of Ini file is like this:

[SectionName]
Key=Value

If you triyng to get some values of file that looks like this:

Text
Key=Value

Then as JdeB says, you will need to read the file and find needed string (maybe StringRegExp will help here).


 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

Well it is mostly an ini file, but also includes key/value pairs before the first section is declared. The application that uses it is not my own, so I have no control over that. But I want to read from it, so I've come up with this workaround...

Func _IniRead($filename, $section, $key, $default)
    If $section = "" Then
        Local $retValue = $default
        Local $i = 0
        Local $fileText = StringSplit(FileRead($filename),@CRLF)
        Local $keyValuePair
        While $i < $FileText[0] and StringLeft(StringStripWS($FileText[$i],3),1) <> "["
            $i = $i + 1
            If StringLeft($FileText[$i],StringLen($key)) = $key Then
                ;Lets check this line...
                $keyValuePair = StringSplit($FileText[$i],"=")
                If $keyValuePair[0] > 1 and StringStripWS($keyValuePair[1],3) = $key Then
                    Return StringStripWS($keyValuePair[2],3)
                EndIf
            EndIf
        WEnd
        Return $retValue
    Else
        return IniRead($filename, $section, $key, $default)
    EndIf
EndFunc

It needs some tweaking, but it works for me. When $section = "" then it searches through the unsectioned top of the file manually, but it wont work too well if the value contains any '=' signs.

Share this post


Link to post
Share on other sites

In fact I came up with a better solution, just create a temp file with a [default] section at the top (or some label that will pretty much always be unique) and work with this. Then if you need to make changes it's easy. Once you want to commit the changes, delete the original ini file and copy the contents of the temp file minus the first [default] section line. This way makes it easier for autoit scripting and the ini's owner application will be none the wiser.

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