Jump to content
Sign in to follow this  
Maikey81

Default value when reading INI key

Recommended Posts

Maikey81

Hi,

I may be overlooking something real simple, but I can't seem to figure it out.

I'm trying the simplest of examples from the autoit site, IniSectionReadNames en combining this with IniRead.

Below is the code:

Local $var = IniReadSectionNames("c:\servers.ini")
If @error Then
    MsgBox(4096, "", "Error occurred, probably no INI file.")
Else
    For $i = 1 To $var[0]
         $ip = IniRead($var, $var[$i], "IP", "unknown")
         MsgBox(4096, "", $var[$i] & " has IP-address: " & $ip)
    Next
EndIf

When executing this file, it does read the section names without any problems but it won't return the value of the IP key. It always returns the default value.

Below is the contents of the ini file:

[Server1]
IP=address
[Server2]
IP=test

Any ideas whats going on?

Share this post


Link to post
Share on other sites
ripdad

Local $ip, $sFile = "c:\servers.ini"
Local $aSection = IniReadSectionNames($sFile)
If @error Then
    MsgBox(4096, "", "Error occurred, probably no INI file.")
Else
    For $i = 1 To $aSection[0]
         $ip = IniRead($sFile, $aSection[$i], "IP", "unknown")
         MsgBox(4096, "", $aSection[$i] & " has IP-address: " & $ip)
    Next
EndIf


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
Maikey81
Local $ip, $sFile = "c:\servers.ini"
Local $aSection = IniReadSectionNames($sFile)
If @error Then
    MsgBox(4096, "", "Error occurred, probably no INI file.")
Else
    For $i = 1 To $aSection[0]
         $ip = IniRead($sFile, $aSection[$i], "IP", "unknown")
         MsgBox(4096, "", $aSection[$i] & " has IP-address: " & $ip)
    Next
EndIf

 

Thanks ripdad! But I don't seem to understand what the big difference is between your and my code.

The only difference i see, is that you declare the ini file in a separate variable before reading the ini file.

Share this post


Link to post
Share on other sites
water

The problem is with the first parameter you specified for IniRead. It has to be the file to be read. You specified the array of section names as returned by InIReadSectionNames.

IniRead($var, ...)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
orbs

@Maikey81, pay close attention to your IniRead() first parameter.

also make yourself a habit to name your variables with meaningful names, it is very helpful in long scripts.

Share this post


Link to post
Share on other sites
ripdad

The main difference is in the parameters.

Your code:

$ip = IniRead($var, $var[$i], "IP", "unknown")

In parameter #1, $var is the entire array -- and not a string path to your ini.

Arrays and strings don't mix well in this instance, unless you specifically call an element, like you did in parameter #2.


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
Maikey81

Ah... now that I see all your replies, it becomes clear.

Thank you all for your replies. It's all clear to me now :)

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  

×