Jump to content

.ini file read into an array


Recommended Posts

This is most likely a newbie question, I have looked but so far have not come across and explanation I understand.

I wish to import a hosts and IP address into an array via an host.ini file, the array will consist of host-name and IP (host-name,IP) now the issue I have is, I obviously do not know the size of the array but yet I have to declare it, how does that work?

The code that I am using is below works as standalone but I need incorporate into the whole, I have also attached the script I am currently working on just in case it may prove useful in the understanding of what i want to do, its still a long way from being completed but it works, any advice would be appreciated.

Func read_hosts_file()
 ; host file sample (hosts.ini) placed on the desktop
 ; [host_servers]
 ; scalaserver  = 10.0.0.1
 ; serverbkuphq  = 10.0.0.4
 ; carvm1   = 10.0.0.6
 ; intranetserver = 10.0.0.8
 ; server1   = 10.0.0.9

 If FileExists($hostfile) Then
  $var = IniReadSection($hostfile, "host_servers")
  If @error Then
   MsgBox(4096, "Error", "Unable to read section.")
  Else
   For $i = 1 To $var[0][0]
    ;RunWait($var[$i][1], @SW_HIDE)
    MsgBox(4096, "", "Key: " & $var[$i][0] & @CRLF & "Value: " & $var[$i][1])
   Next
  EndIf
 Else
  MsgBox(4096, "Host File Error", "The INI file does not exist.")
 EndIf
EndFunc   ;==>read_hosts_file

This code is most likly not upto standard I have only been doing this for a few days now, in fact this is my first script and just thought well what the heck use autoit, the Forums have been most helpfull.

rsd-working_array.au3

Edited by PeterAtkin

[topic='115020'] AD Domain Logon Script[/topic]

Link to comment
Share on other sites

OK done, solved..

Func read_host_file_into_array()
; host file sample (hosts.ini) placed on the desktop
If FileExists($hostfile) Then
  $var = IniReadSection($hostfile, "host_servers")
  If @error Then
   MsgBox(4096, "Error", "Unable to read section.")
  Else
   For $i = 1 To $var[0][0]
    $serverArr[$i][1] = $var[$i][1]
    $serverArr[$i][0] = $var[$i][0]
    ; MsgBox(4096, "", "Key: " & $var[$i][0] & @CRLF & "Value: " & $var[$i][1])
   Next
   $sh = UBound($var) - 1; get zize of array just take some leg work out of the for next loops, i'm quit lazy
  EndIf
Else
  MsgBox(4096, "Host File Error", "The INI file does not exist.")
EndIf
EndFunc   ;==>read_host_file_into_array

I have also attached the updated Autoit script that I believe will be a good pointer for others as this was my first script and covered lots of basic principles (i hope).. enjoy.

rsd-working.au3

Edited by PeterAtkin

[topic='115020'] AD Domain Logon Script[/topic]

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