Jump to content

Default value when reading INI key


 Share

Go to solution Solved by ripdad,

Recommended Posts

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?

Link to comment
Share on other sites

  • Solution

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

@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.

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

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

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...