Jump to content

IniRead(ENTIRE)Section


Recommended Posts

Hey there.

I'm starting a script to use as a server index. We got more than a thousand servers here, and each one is for a different application. Sure we got a Excel File documenting them all, but I wanted to make it practical and simple. Here is the idea. Someone will type the name of the server (GUI Will be developed later) and it'll read an iniSection entirely and return a splashscreen with the information on chosen server.

But I can't figure out how to have iniReadSection read the entire section and display it. I have dimmed the script for you guys to analyse only the function.

Here it is:

;$Servername = IniRead(@ScriptDir & "\servername.ini", "Servernames", "Server", "")

$Servername = "ServerTest"
$Description = IniReadSection(@ScriptDir & "\ServersINI.ini", "SERVERtest1")
SplashTextOn($Servername, $Description)
Sleep(3000)
SplashOff()

Here is the content of ServersINI.ini:

[SERVERtest1]
________________________________________
Description:
--------------------


________________________________________
OS:
--------------------


________________________________________
IP Address:
--------------------

________________________________________
Comments:
--------------------



[SERVERtest2]

...
...
...

Any ideas? Could be any other way, it doesn't have to be a ini read specifically. I'm trying to make it work through StdOutread, for example.

Link to comment
Share on other sites

Well, could be. Some of the lines, like comments and description could be more then a line long and easier to perform maintenance (editting) anyway, that would be a good suggestion. As I see, it seems to be impossible to display the entire section, right?

Link to comment
Share on other sites

First have you tried writing all that data into an ini file yet? You may well run into ini file limitations with that many servers to check.

I would take a serious look at reading the data into an SQLite database (in memory perhaps) and working from that.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Maybe you can try this:

#Include <File.au3>
#Include <Array.au3>

Local $CfgFile = @ScriptDir & '\Servers.cfg'
Local $AllSectionNames = IniReadSectionNames($CfgFile)
If NOT @error then _ArrayDisplay($AllSectionNames, 'all section names')
Local $KeysAndValues = _CfgReadSection($CfgFile, 'SERVERtest1'); $var[0][0] - total number of keys, $var[$i][0] = key name, $var[$i][1] = key value
If NOT @error then _ArrayDisplay($KeysAndValues, 'Section SERVERtest1')
MsgBox(0, 'Read one key', 'OS: ' & _CfgReadValue($CfgFile, 'SERVERtest1', 'OS'))

Func _CfgReadValue($iFileName, $iSection, $iKey)
    If NOT FileExists($iFileName) then Return SetError(1) ; file not exist
    Local $FileLines
    Local $SectionName
    Local $StartReading = False, $ReadNextLine = False, $ReadNextData = False, $ReadThisKey = False
    Local $RetVal
    Local $TotalData
    _FileReadToArray($iFileName, $FileLines)
    For $i = 1 to $FileLines[0]
        If $StartReading = True Then
            If StringRegExp($FileLines[$i], '\[(.*?)\]', 0) then ExitLoop
            If $ReadNextLine = True Then
                $ReadNextLine = False
                $SectionName = StringRegExp($FileLines[$i], '(.*?)\:', 1)
                If UBound($SectionName) <> 1 then Return SetError(2) ;invalid section name
                If $SectionName[0] = $iKey then $ReadThisKey = True
            EndIf
            If $FileLines[$i] = '________________________________________' Then
                $ReadNextLine = True
                $ReadNextData = False
                If $ReadThisKey = True then ExitLoop
            EndIf
            If $ReadNextData = True then $RetVal &= $FileLines[$i]
            If ($FileLines[$i] = '--------------------') AND ($ReadThisKey = True) Then $ReadNextData = True
        EndIf
        If ($StartReading = False) AND (StringRegExp($FileLines[$i], '\[(.*?)\]', 0)) then
            $SectionName = StringRegExp($FileLines[$i], '\[(.*?)\]', 1)
            If UBound($SectionName) <> 1 then Return SetError(2) ;invalid section name
            If $SectionName[0] = $iSection then $StartReading = True
        EndIf
    Next
    Return $RetVal
EndFunc

Func _CfgReadSection($iFileName, $iSection)
    If NOT FileExists($iFileName) then Return SetError(1) ; file not exist
    Local $FileLines
    Local $SectionName
    Local $StartReading = False, $ReadNextLine = False, $ReadNextData = False
    Local $RetVal[1][2]
    $RetVal[0][0] = 0
    Local $TotalData
    _FileReadToArray($iFileName, $FileLines)
    For $i = 1 to $FileLines[0]
        If $StartReading = True Then
            If StringRegExp($FileLines[$i], '\[(.*?)\]', 0) then ExitLoop
            If $ReadNextLine = True Then
                $ReadNextLine = False
                $RetVal[0][0] += 1
                ReDim $RetVal[UBound($RetVal)+1][2]
                $SectionName = StringRegExp($FileLines[$i], '(.*?)\:', 1)
                If UBound($SectionName) <> 1 then Return SetError(2) ;invalid section name
                $RetVal[UBound($RetVal)-1][0] = $SectionName[0]
            EndIf
            If $FileLines[$i] = '________________________________________' Then
                $ReadNextLine = True
                $ReadNextData = False
            EndIf
            If $ReadNextData = True then $RetVal[UBound($RetVal)-1][1] &= $FileLines[$i]
            If $FileLines[$i] = '--------------------' Then $ReadNextData = True
        EndIf
        If ($StartReading = False) AND (StringRegExp($FileLines[$i], '\[(.*?)\]', 0)) then
            $SectionName = StringRegExp($FileLines[$i], '\[(.*?)\]', 1)
            If UBound($SectionName) <> 1 then Return SetError(2) ;invalid section name
            If $SectionName[0] = $iSection then $StartReading = True
        EndIf
    Next
    Return $RetVal
EndFunc

and I used this as sample for Servers.cfg file:

[SERVERtest1]
________________________________________
Description:
--------------------
Some description


________________________________________
OS:
--------------------
windows XP SP3


________________________________________
IP Address:
--------------------
192.168.1.100


________________________________________
Comments:
--------------------
Some random comment


[SERVERtest2]
...
...
...
Edited by dragan
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...