jdillig

IniRead() Not Finding Values

27 posts in this topic

Gentlemen,

Hopefully you can help me out here. I have been beating my head against a wall for about a week trying to figure this one out before writing this.

I have a simple setup where i am storing information inside a .ini file and using the IniRead() function to pull this information. I have done this in the past and not had an issue until now. The $Lab_LDConfig_Descript variable always will equal the "0" default value and i cannot figure out why. I can take the full path (from the root) and past into CMD and it will find and open the .ini so I do not believe it is an issue with the path. I have also tried UTF and ANSI encoding on the file and this did not change anything.

Hopefully this is something stupid and simple but I cannot find it!

 

Here is the basic code:

$Lab_LDConfig_Descript = IniRead(@ScriptDir&"\labs\R77.20 Standalone\LDConfig.ini","Lab Description","Description",0)

    if $Lab_LDConfig_Descript = 0 Then

        MsgBox(0,"Error!","Iniread returned 0!")
    Else
        MsgBox(0,"Success!","Iniread returned "&$Lab_LDConfig_Descript)
    EndIf

The LDConfig.ini file contains the following:

[Lab Description]
Description=This is a lab
[OVF Instructions]
OVFDeploy=instructions here

 

Share this post


Link to post
Share on other sites



jdillig,

Welcome to the AutoIt forums. :)

IniRead returns a string - you are comparing it to a number. Change the line to read:

if $Lab_LDConfig_Descript = "0" Then
and all will be well. Basically mixing numbers and strings in comparisons is a good recipe for tears at bedtime. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

jdillig,

Welcome to the AutoIt forums. :)

IniRead returns a string - you are comparing it to a number. Change the line to read:

if $Lab_LDConfig_Descript = "0" Then
and all will be well. Basically mixing numbers and strings in comparisons is a good recipe for tears at bedtime. ;)

M23

 

 

Melba23,

Thanks for the response. I tried this and found no difference. I am expecting the $Lab_LDConfig_Descript variable to contain the string, "This is a lab" as read from the LDConfig.ini file.

When I output what $Lab_LDConfig_Descript contains into a msgbox, I still get "0".

Share this post


Link to post
Share on other sites

jdillig,

Well, it works for me because I created the ini file and path as you showed in the OP and ran the code before posting. ;)

To explain why I suggested what I did: you were using a comparison with mixed datatypes - in this case AutoIt does an internal conversion so they are the same type, usually to numeric values. Thus the string "This is a lab" is converted to 0 - as it is not a number - and this matched the numeric 0 on the other side. Converting both sides to strings (by adding quotes around the "0") made the comparison valid and the correct MsgBox appeared with the correct value. :)

Now if you say that IniRead is actually not reading the ini value at all then I can only suggest that the file is somehow not accessible. Try adding a FileExists with the same path just before reading and see what is returned. :wacko:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Good evening. I Tried your script with a few modifications ... take a look:

$Lab_LDConfig_Descript = IniRead(@ScriptDir & "\LDConfig.ini", "Lab Description", "Description", "ERROR")
if $Lab_LDConfig_Descript = "ERROR" Then
    MsgBox(0, "Error!", "Iniread returned ERROR!")
Else
    MsgBox(0, "Success!", "Iniread returned " & $Lab_LDConfig_Descript)
EndIf

For me worked fine, but I changed both the path and the default value returned by IniRead.

Try change or verifying the path of ini file

Share this post


Link to post
Share on other sites

Out of curiosity, what is the size of the ini file?


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.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

jdillig,

Well, it works for me because I created the ini file and path as you showed in the OP and ran the code before posting. ;)

To explain why I suggested what I did: you were using a comparison with mixed datatypes - in this case AutoIt does an internal conversion so they are the same type, usually to numeric values. Thus the string "This is a lab" is converted to 0 - as it is not a number - and this matched the numeric 0 on the other side. Converting both sides to strings (by adding quotes around the "0") made the comparison valid and the correct MsgBox appeared with the correct value. :)

Now if you say that IniRead is actually not reading the ini value at all then I can only suggest that the file is somehow not accessible. Try adding a FileExists with the same path just before reading and see what is returned. :wacko:

M23

 

I added a fileexist() check and it found it ok. Below is the code I used.

 

 

Good evening. I Tried your script with a few modifications ... take a look:

$Lab_LDConfig_Descript = IniRead(@ScriptDir & "\LDConfig.ini", "Lab Description", "Description", "ERROR")
if $Lab_LDConfig_Descript = "ERROR" Then
    MsgBox(0, "Error!", "Iniread returned ERROR!")
Else
    MsgBox(0, "Success!", "Iniread returned " & $Lab_LDConfig_Descript)
EndIf

For me worked fine, but I changed both the path and the default value returned by IniRead.

Try change or verifying the path of ini file

 

I tested your method and it did work when the .ini file was in the same directory as the script. Does anyone know if the IniRead() function relies on a .ini file path that does not contain any spaces?

 

 

Out of curiosity, what is the size of the ini file?

 

The test .ini is only 4 lines (97 bytes) and the full .ini is around 20-30 lines (496 bytes)

 

 

This is the code I used to test with the suggestions so far. It still does not work when the .ini file is not in the same directory as the script.

if FileExists(@ScriptDir & "\labs\R77.20 Standalone\LDConfig.ini") then MsgBox(0,"","It is there")
$Lab_LDConfig_Descript = IniRead(@ScriptDir & "\labs\R77.20 Standalone\LDConfig.ini", "Lab Description", "Description", "ERROR")

if $Lab_LDConfig_Descript = "ERROR" Then
    MsgBox(0, "Error!", "Iniread returned ERROR!")
Else
    MsgBox(0, "Success!", "Iniread returned " & $Lab_LDConfig_Descript)
EndIf
Edited by jdillig

Share this post


Link to post
Share on other sites

Try renaming that folder "R77.20 Standalone" removing the . and see if it works.

 

Same issue. FileExists() finds the file but IniRead() does not.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

jdillig,

 

Does anyone know if the IniRead() function relies on a .ini file path that does not contain any spaces?

There is no problem with IniRead and spaces - I created the same folder structure as in your example and it worked without problem. :)

Let us try another test. Try using this and see what you get:

; Add this at the top of your script
#include <Array.au3>

; Then we try to read all section names in the ini file
$aSections = IniReadSectionNames(@ScriptDir & "\labs\R77.20 Standalone\LDConfig.ini")
_ArrayDisplay($aSections)

; And then the specific section in which we are interested
$aSection = IniReadSection(@ScriptDir & "\labs\R77.20 Standalone\LDConfig.ini", "Lab Description")
_ArrayDisplay($aSection)
Needless to say that works for me - any luck at your end? :huh:

M23

Edit:

And I have lifted your "New Member" first day post limit - I think we might need a while to get to the bottom of this! ;)

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Gentlemen,

The LDConfig.ini file contains the following:

[Lab Description]
Description=This is a lab
[OVF Instructions]
OVFDeploy=instructions here

Global $sFileINI=@ScriptDir & "\labs\R77.20 Standalone\LDConfig.ini"; @ScriptDir & "\LDConfig.ini" 

If Not FileExists($sFileINI) Then 
    MsgBox(0,"Error!","INI File Not Found!"& @CRLF &$sFileINI)
Else
     Local $Lab_LDConfig_Descript = IniRead($sFileINI, "Lab Description", "Description", "Error")

     If ($Lab_LDConfig_Descript<>"Error") Then
        MsgBox(0, "Success!", "Iniread returned " & $Lab_LDConfig_Descript)
     Else
          MsgBox(0, "Error!", "Iniread returned ERROR!")
     EndIf
EndIf
Edited by Trong

Regards,
 

Share this post


Link to post
Share on other sites

jdillig,

 

There is no problem with IniRead and spaces - I created the same folder structure as in your example and it worked without problem. :)

Let us try another test. Try using this and see what you get:

; Add this at the top of your script
#include <Array.au3>

; Then we try to read all section names in the ini file
$aSections = IniReadSectionNames(@ScriptDir & "\labs\R77.20 Standalone\LDConfig.ini")
_ArrayDisplay($aSections)

; And then the specific section in which we are interested
$aSection = IniReadSection(@ScriptDir & "\labs\R77.20 Standalone\LDConfig.ini", "Lab Description")
_ArrayDisplay($aSection)
Needless to say that works for me - any luck at your end? :huh:

M23

Edit:

And I have lifted your "New Member" first day post limit - I think we might need a while to get to the bottom of this! ;)

 

 

So this is interesting, the _arraydisplay() only shows 1 of the 2 sections inside the .ini. The one shown however is not the one I called from IniRead(), it finds the "OVF Instructions" only.

_arraydisplay()

Row|Col 0
[0]|1
[1]|OVF Instructions
 

 

I don't think that the .ini format is incorrect... Maybe it is?

Share this post


Link to post
Share on other sites
Global $sFileINI=@ScriptDir & "\labs\R77.20 Standalone\LDConfig.ini"; @ScriptDir & "\LDConfig.ini" 

If Not FileExists($sFileINI) Then MsgBox(0,"Error!","INI File Not Found!"& @CRLF &$sFileINI)

Local $Lab_LDConfig_Descript = IniRead($sFileINI, "Lab Description", "Description", "Error")

If ($Lab_LDConfig_Descript<>"Error") Then
    MsgBox(0, "Success!", "Iniread returned " & $Lab_LDConfig_Descript)
Else
     MsgBox(0, "Error!", "Iniread returned ERROR!")
EndIf

Trong,

Same issue, no change

Share this post


Link to post
Share on other sites

Trong,

Same issue, no change

Global $sFileINI=@ScriptDir & "\labs\R77.20 Standalone\LDConfig.ini"; <<<<<<<<<<<<<<<<<<<<<<<<

If Not FileExists($sFileINI) Then
    MsgBox(0,"Error!","INI File Not Found!"& @CRLF &$sFileINI)
Else
    Local $Lab_LDConfig_Descript = IniRead($sFileINI, "Lab Description", "Description", "Error")
    Local $LOVF_Instructions_OVFDeploy = IniRead($sFileINI, "OVF Instructions", "OVFDeploy", "Error")

    If ($Lab_LDConfig_Descript<>"Error") Then
        MsgBox(0, "Success!", "Lab LDConfig Descript: " & $Lab_LDConfig_Descript& @CRLF & "LOVF Instructions OVFDeploy: " & $LOVF_Instructions_OVFDeploy)
    Else
         MsgBox(0, "Error!", "Iniread returned ERROR!")
    EndIf
EndIf

Regards,
 

Share this post


Link to post
Share on other sites

jdillig,

We progress! :party:

The problem is obviously in the ini file itself. I just copy-pasted what you had in the OP into my file - I suggest you do the same and see if you can read it then. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

I have a feeling this thread is going to go another 5 pages then...

oops sorry, I thought I was using AutoIt but I was actually painting my dog.

Solved.

EDIT:

Or not :)

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

This works...

$Lab_LDConfig_Descript = IniRead(@ScriptDir & "\labs\R77.20 Standalone\LDConfig.ini", "Lab Description", "Description", 0)

ConsoleWrite($Lab_LDConfig_Descript & @CRLF)

If $Lab_LDConfig_Descript = "0" Then

    MsgBox(0, "Error!", "Iniread returned 0!")
Else
    MsgBox(0, "Success!", "Iniread returned " & $Lab_LDConfig_Descript)
EndIf

The comparison to a numeric 0 was wrong...

Return Value
Success: the requested key value as a string. 
Failure: the default string if requested key not found.
Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

If the last solution provided by Melb23 does not do the job (I'm imagining you may have a bracket error or something like that), then can you open your INI file in Notepad and then take a screenshot and upload here for us to check.


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

Dude, the OP was comparing the "string" returned from the iniread to a numeric 0...see post above...


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

We worked that out at Post #2.

All the examples since have been working for everyone else, but not OP.

Hence why we think something is wrong inside his INI file, and it is not exactly as shown.

Edited by TheSaint

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

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