Jump to content

Recommended Posts

Hi,

I've written a specialty INI file editor for a specific set of INI files related to a bigger script I'm working on.

I'm trying to create a verification script that checks the ini files structure and format and gives an error if they are not in expected format.

What I did so far is create a couple of custom arrays, each with the list of Sections expected in the files and I use IniReadSectionNames in order to read the sections from the selected INI file and compare it to the array.

It works fine, excepts it only gives an error if one or more of the sections in the custom array is missing, if they all exist but there are other additional sections that shouldn't be there it doesn't give an error and continue to open the editor screen.

Here are the relevant part of my script:

The Custom Verification Arrays:

;Creating Arrays for INI Verification
Global $aSettingsVerify[7]
$aSettingsVerify[1] = "Conversion"
$aSettingsVerify[2] = "AIO"
$aSettingsVerify[3] = "Data"
$aSettingsVerify[4] = "Redist"
$aSettingsVerify[5] = "Split"
$aSettingsVerify[6] = "Autorun"
Global $aDataVerify[6]
$aDataVerify[1] = "Compression"
$aDataVerify[2] = "Exclude"
$aDataVerify[3] = "LangExclude"
$aDataVerify[4] = "PreCommands"
$aDataVerify[5] = "PostCommands"

The IniReadSectionNames verification part:

Case $OK
                If StringInStr($ActiveConfig, "Settings.ini") Then
                    $CheckArray1 = IniReadSectionNames($ActiveConfig)
                    $CheckArray2 = _ArrayCompare($aSettingsVerify, $CheckArray1)
                    $CheckArray3 = UBound($CheckArray2, 1)
                    If $CheckArray3 = 1 Then
                        GUIDelete($ConfigStart)
                        SettingsGUI()
                    EndIf
                    If $CheckArray3 > 1 Then
                        WrongIni()
                    EndIf
                EndIf
                If StringInStr($ActiveConfig, "Data") Then
                    $CheckArray1 = IniReadSectionNames($ActiveConfig)
                    $CheckArray2 = _ArrayCompare($aDataVerify, $CheckArray1)
                    $CheckArray3 = UBound($CheckArray2, 1)
                    If $CheckArray3 = 1 Then
                        GUIDelete($ConfigStart)
                        DataGUI()
                    EndIf
                    If $CheckArray3 > 1 Then
                        WrongIni()
                    EndIf
                EndIf

The Function that performs the compare: (Found it in another thread on this forums somewhere by using Google Search)

;Compares Imported INI file with the Verification array
Func _ArrayCompare(ByRef $a1, ByRef $a2)
    Local $nOldSize = UBound($a2)
    Local $a3[$nOldSize], $nNewSize = $nOldSize

    For $i = 0 To UBound($a1) - 1
        For $j = 0 To $nOldSize - 1
            If Not $a3[$j] And ($a1[$i] = $a2[$j]) Then
                $a3[$j] = 1
                $nNewSize -= 1
            EndIf
        Next
    Next

    Local $a4[$nNewSize], $j = 0
    For $i = 0 To $nOldSize - 1
        If Not $a3[$i] Then
            $a4[$j] = $a2[$i]
            $j += 1
        EndIf
    Next

    Return $a4
EndFunc   ;==>_ArrayCompare

 

Any way to make a check if there are sections other than the ones in the Verification Array and produce an error?

Thanks


Ron Vollach
Microsoft Certified Professional (MCP)
Creator of Ultimate Conversion Compressor (UCC)
UCC Wikia

Share this post


Link to post
Share on other sites

VollachR,

Run a check of the size of the array returned by IniReadSectionNames - if it is larger than the checking array there are too many entries.

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

As Melba23 pointed out you can compare section name count for example:

If UBound($CheckArray1) - 1 <> UBound($aSettingsVerify) - 1 Then ... ;~ Action here

Alternatively you can get an array of incorrect names and also compare the sizes for example:

#include <Array.au3>
Global $aVerify[7]
$aVerify[1] = "Conversion"
$aVerify[2] = "AIO"
$aVerify[3] = "Data"
$aVerify[4] = "Redist"
$aVerify[5] = "Split"
$aVerify[6] = "Autorun"

;~ IniReadSection
Global $aSection[9]
$aSection[1] = "Conversion"
$aSection[2] = "AIO"
$aSection[3] = "Data"
$aSection[4] = "Redist"
$aSection[5] = "Split"
$aSection[6] = "Autorun"
$aSection[7] = "AutoRunning"
$aSection[8] = "Confersion"

Global $aCompare = _SectionCompare($aSection, $aVerify)
_ArrayDisplay($aCompare, "Error Return = " & @error)

Func _SectionCompare($_aSections, $_aVerification)
    Local $_iError, $_iSearch
    If (UBound($_aSections) -1) < (UBound($_aVerification) - 1) Then $_iError = -1 ;~ $_aSections Array Count less than $_aVerification Array Count
    If (UBound($_aSections) -1) = (UBound($_aVerification) - 1) Then $_iError = 0 ;~ $_aSections Array Count equal to $_aVerification Aay Count
    If (UBound($_aSections) -1) > (UBound($_aVerification) - 1) Then $_iError = 1 ;~ $_aSections Array Count greater than $_aVerification Array Count
    ;~ Check items in $_aSections that match an item in $_aVerification
    For $i = UBound($_aVerification) - 1 To 0 Step - 1
        ConsoleWrite($_aVerification[$i] & @CRLF)
        $_iSearch = _ArraySearch($_aSections, $_aVerification[$i])
        If $_iSearch = -1 Then ContinueLoop
        _ArrayDelete($_aSections, $_iSearch)
    Next
    Return SetError($_iError, 0, $_aSections)
EndFunc

 

Share this post


Link to post
Share on other sites
21 minutes ago, Melba23 said:

VollachR,

Run a check of the size of the array returned by IniReadSectionNames - if it is larger than the checking array there are too many entries.

M23

I thought about that but I'm not sure how to modify the existing function to include that check, as I said, I found that function online, don't remember where or who wrote it but I didn't do it myself, all I did was remove the _ArrayDisplay that was near the end of the function.

 

5 minutes ago, Subz said:

As Melba23 pointed out you can compare section name count for example:

If UBound($CheckArray1) - 1 <> UBound($aSettingsVerify) - 1 Then ... ;~ Action here

Alternatively you can get an array of incorrect names and also compare the sizes for example:

#include <Array.au3>
Global $aVerify[7]
$aVerify[1] = "Conversion"
$aVerify[2] = "AIO"
$aVerify[3] = "Data"
$aVerify[4] = "Redist"
$aVerify[5] = "Split"
$aVerify[6] = "Autorun"

;~ IniReadSection
Global $aSection[9]
$aSection[1] = "Conversion"
$aSection[2] = "AIO"
$aSection[3] = "Data"
$aSection[4] = "Redist"
$aSection[5] = "Split"
$aSection[6] = "Autorun"
$aSection[7] = "AutoRunning"
$aSection[8] = "Confersion"

Global $aCompare = _SectionCompare($aSection, $aVerify)
_ArrayDisplay($aCompare, "Error Return = " & @error)

Func _SectionCompare($_aSections, $_aVerification)
    Local $_iError, $_iSearch
    If (UBound($_aSections) -1) < (UBound($_aVerification) - 1) Then $_iError = -1 ;~ $_aSections Array Count less than $_aVerification Array Count
    If (UBound($_aSections) -1) = (UBound($_aVerification) - 1) Then $_iError = 0 ;~ $_aSections Array Count equal to $_aVerification Aay Count
    If (UBound($_aSections) -1) > (UBound($_aVerification) - 1) Then $_iError = 1 ;~ $_aSections Array Count greater than $_aVerification Array Count
    ;~ Check items in $_aSections that match an item in $_aVerification
    For $i = UBound($_aVerification) - 1 To 0 Step - 1
        ConsoleWrite($_aVerification[$i] & @CRLF)
        $_iSearch = _ArraySearch($_aSections, $_aVerification[$i])
        If $_iSearch = -1 Then ContinueLoop
        _ArrayDelete($_aSections, $_iSearch)
    Next
    Return SetError($_iError, 0, $_aSections)
EndFunc

 

I don't see how the 2nd example will work, if somebody modifies the ini file and add a section that shouldn't be there I have no way to guess what section name that person added, so I can't really create an array with incorrect names as that can be anything that isn't in the verification array.

As for the first suggestion, as I said above for Melba23's answer, I'm not sure where or how to add that into the existing _ArrayCompare function, or is there away to add it in the part of the script that calls the function without messing things up?


Ron Vollach
Microsoft Certified Professional (MCP)
Creator of Ultimate Conversion Compressor (UCC)
UCC Wikia

Share this post


Link to post
Share on other sites

The $aSection above was an example of an IniReadSection, it should return the differences, here is another example since I didn't  make the first a 2D Array and forgot about comparing the results after the loop.  As I only have partial piece of your script I can't test so will leave it with you.

#include <Array.au3>

Global $aSettingsVerify[7]
    $aSettingsVerify[1] = "Conversion"
    $aSettingsVerify[2] = "AIO"
    $aSettingsVerify[3] = "Data"
    $aSettingsVerify[4] = "Redist"
    $aSettingsVerify[5] = "Split"
    $aSettingsVerify[6] = "Autorun"

Global $aDataVerify[6]
    $aDataVerify[1] = "Compression"
    $aDataVerify[2] = "Exclude"
    $aDataVerify[3] = "LangExclude"
    $aDataVerify[4] = "PreCommands"
    $aDataVerify[5] = "PostCommands"

If StringInStr($ActiveConfig, "Settings.ini") Then
    $CheckArray1 = IniReadSectionNames($ActiveConfig)
    $CheckArray2 = _SectionCompare($CheckArray1, $aSettingsVerify)
    Switch @error
        Case -2, -1, 1
            GUIDelete($ConfigStart)
            SettingsGUI()
    EndSwitch
    _ArrayDisplay($CheckArray2, "Error Return = " & @error)
EndIf
If StringInStr($ActiveConfig, "Data") Then
    $CheckArray1 = IniReadSectionNames($ActiveConfig)
    $CheckArray2 = _SectionCompare($CheckArray1, $aDataVerify)
    Switch @error
        Case -2, -1, 1
            GUIDelete($ConfigStart)
            SettingsGUI()
    EndSwitch
    _ArrayDisplay($CheckArray2, "Error Return = " & @error)
EndIf

; ==================================================================================================
; Func _SectionCompare($_aSections, $_aVerification)
;
; $_aSections       := Section Names Array
; $_aVerifcation    := Verification Array
;
; Returns:
;   Success: "" with @error set to 0
;   Failure: "" if $_aSections is empty or $_vResult Array with differnces, with @error set
;       @error = -2 = $_aSections Array contains incorrect data
;       @error = -1 $_aSections Array Count less than $_aVerification Array Count
;       @error = 1 $_aSections Array Count greater than $_aVerification Array Count
;
; Author: Subz
; ==================================================================================================

Func _SectionCompare($_aSections, $_aVerification)
    Local $_iError, $_iSearch
    If (UBound($_aSections) -1) < (UBound($_aVerification) - 1) Then $_iError = -1 ;~ $_aSections Array Count less than $_aVerification Array Count
    If (UBound($_aSections) -1) = (UBound($_aVerification) - 1) Then $_iError = 0 ;~ $_aSections Array Count equal to $_aVerification Aay Count
    If (UBound($_aSections) -1) > (UBound($_aVerification) - 1) Then $_iError = 1 ;~ $_aSections Array Count greater than $_aVerification Array Count
    ;~ Check items in $_aSections that match an item in $_aVerification
    For $i = UBound($_aVerification) - 1 To 0 Step - 1
        $_iSearch = _ArraySearch($_aSections, $_aVerification[$i], 0, 0, 0, 0, 1, 0)
        If $_iSearch = -1 Then ContinueLoop
        _ArrayDelete($_aSections, $_iSearch)
    Next
    $_iError = UBound($_aSections) -1 = 0 ? 0 : -2
    $_vReturn = UBound($_aSections) = 0 ? "" : $_aSections
    Return SetError($_iError, 0, $_vReturn)
EndFunc

 

Share this post


Link to post
Share on other sites

OK, turned out I didn't need that additional check after all, my existing check was enough once I moved another part of the script from another "Case" into the "Case $OK" portion, it was probably a logic issue.

I accidentally made the script import the in sections (with IniReadSection) to Arrays before performing the check instead of after, which caused my problem, now that I moved it to be done after the check is performed I get the error message at every situation it needs to be without any additional checks.

That's the change I made:

Case $OK
                If StringInStr($ActiveConfig, "Settings.ini") Then
                    $CheckArray1 = IniReadSectionNames($ActiveConfig)
                    $CheckArray2 = _ArrayCompare($aSettingsVerify, $CheckArray1)
                    $CheckArray3 = UBound($CheckArray2, 1)
                    If $CheckArray3 = 1 Then
                        Global $aConversion = IniReadSection($ActiveConfig, "Conversion")
                        Global $aAIO = IniReadSection($ActiveConfig, "AIO")
                        Global $aData = IniReadSection($ActiveConfig, "Data")
                        Global $aRedist = IniReadSection($ActiveConfig, "Redist")
                        Global $aSplit = IniReadSection($ActiveConfig, "Split")
                        Global $aAutorun = IniReadSection($ActiveConfig, "Autorun")
                        GUIDelete($ConfigStart)
                        SettingsGUI()
                    EndIf
                    If $CheckArray3 > 1 Then
                        WrongIni()
                    EndIf
                EndIf
                If StringInStr($ActiveConfig, "Data") Then
                    $CheckArray1 = IniReadSectionNames($ActiveConfig)
                    $CheckArray2 = _ArrayCompare($aDataVerify, $CheckArray1)
                    $CheckArray3 = UBound($CheckArray2, 1)
                    If $CheckArray3 = 1 Then
                        Global $aCompression = IniReadSection($ActiveConfig, "Compression")
                        Global $aExclude = IniReadSection($ActiveConfig, "Exclude")
                        Global $aLangExclude = IniReadSection($ActiveConfig, "LangExclude")
                        Global $aPreCommands = IniReadSection($ActiveConfig, "PreCommands")
                        Global $aPostCommands = IniReadSection($ActiveConfig, "PostCommands")
                        GUIDelete($ConfigStart)
                        DataGUI()
                    EndIf
                    If $CheckArray3 > 1 Then
                        WrongIni()
                    EndIf
                EndIf

Before I had these IniReadSection lines in a previous Case for a ComboBox that let the user select the Ini file to edit before enabling the OK button, now it's in the OK button case after the check if it's correct.

These IniReadSection arrays are used to later fill the edit fields in the GUI, don't know why creating them earlier caused a problem but it works now.


Ron Vollach
Microsoft Certified Professional (MCP)
Creator of Ultimate Conversion Compressor (UCC)
UCC Wikia

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

  • Similar Content

    • By _leo_
      Hey guys
      I'm new to autoit, so this could be a simple question. I'm trying to read the value from the currently selected cell in Excel. I read on the forum and tired to find videos, but I couldn't quite get to it. 
      This is what I have got so far:
       
      Local $oExcel_1 = _Excel_Open()
      Local $var = "C:\Users\Acer\xy"
      Local $oWorkbook = _Excel_BookOpen($oExcel_1,$var)
       
      Local $_read1 = _Excel_RangeRead($oWorkbook, Default.Application.ActiveCell.Address)
       
      Whatever I try, I either get an error or it only reads "0".
       
      Thank you very much for any helpful thoughts!
    • By 2Toes
      Hey there,
      I have a program that I'm working on, and I have a button that's supposed to stop a running function, when it's pressed.
      However, I cannot get that to work.
      I put together a sample script that is set up in the same way:
      #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form= Opt("GUIOnEventMode", 1) Global $Form1 = GUICreate("Form1", 290, 106, 192, 124) GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit") Global $btnStart = GUICtrlCreateButton("Start", 24, 24, 99, 49) GUICtrlSetOnEvent($btnStart, "_Start") Global $btnStop = GUICtrlCreateButton("Stop", 152, 24, 99, 49) GUICtrlSetOnEvent($btnStop, "_Stop") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 ;~ $nMsg = GUIGetMsg() ;~ Switch $nMsg ;~ Case $GUI_EVENT_CLOSE ;~ Exit ;~ EndSwitch WEnd Func _Start() Global $bStartFlag = True $num = 1 While $bStartFlag ConsoleWrite($num & @CRLF) Sleep(1000) $num += 1 WEnd ConsoleWrite("Exited loop.." & @CRLF) EndFunc Func _Stop() Global $bStartFlag = False EndFunc Func _Exit() Exit EndFunc  
      With the code above, while the _Start() func is running, the Stop button doesn't do anything.
      I even threw a MsgBox into the _Stop() func, to see if the script was at least accessing the _Stop() func. But that showed that the _Stop() func is not being accessed at all while the _Start() func is running.
      I'm sure this is a simple solution that I'm just over looking. But I can't figure out what that solution is lol.
      Any help here would be greatly appreciated!
      Thank you in advance!  
    • By jantograaf
      Hi all,
      I'm trying to write a script that connects with a VBA/COM API to get the status of a connected phone. I've been looking up and down this forum for tips or other user's experiences, but I can't seem to find anything (even remotely) similar. It shouldn't be so hard to do, however.
      Software I'm trying to connect to
      I'm trying to integrate CallCenter by using their API, which is documented over here : JustRemotePhone API Reference
      Things I've tried
      I've tried using ObjCreate but I don't get any result, it always returns the same (negative) error.
      #Version 1 tried ObjCreate("JustRemotePhone.RemotePhoneService") #Version 2 tried ObjCreate("JustRemotePhoneCOM.RemotePhoneService") #Version 3 tried ObjCreate("JustRemotePhoneCOM.RemotePhoneService.Application") None of the three versions I tried seem to deliver any result other than a negative error value which basically says that the given class is not valid.
      I am starting to get the hang of AutoIt by now, but unmanaged programming languages and object-oriented stuff is still quite a grey zone for me. If anyone could help me 'talk' to this application, I'd be immensely grateful!
      Thanks in advance and kind regards from Belgium!
       
      Jan
    • By nooneclose
      Hello everyone, I am wondering if and how it is possible to connect my AutoIt script to a different email in my outlook. I have two different accounts when I open outlook. The first which my script always connects to is my own email while the second is a group email account. How or can I make the script connect to the second one and access its inbox instead of mine? This is all work related so no funny business here. 
      Global $oOutlook = _OL_Open() ; Store all the Unread emails into an array Global $aItems = _OL_ItemFind($oOutlook, "*\Outlook-UDF-Test", $olMail, _ "[UnRead]=True", "Subject", "Maintenance Request from Eagle's Nest", "EntryID,Subject", "", 1) ; Display the array of unRead emails If IsArray($aItems) Then ;_ArrayDisplay($aItems, "OutlookEX UDF: _OL_ItemFind - Unread mails") Else MsgBox(48, "OutlookEX UDF: _OL_ItemFind Example Script", _ "Could not find an unread mail. @error = " & @error & ", @extended: " & @extended) EndIf ; Get the number of unread emails Global $numberOfUnRead = UBound($aItems, $UBOUND_ROWS) - 1 ;MsgBox("", "Number of Unread emails", $numberOfUnRead) ConsoleWrite("Number of unread emails: " & $numberOfUnRead & @CRLF) I might be missing some code in the post but my code works It's just so long I don't want to post all 2300+ lines. 
      Thank you for the help I greatly appreciate this community. 
    • By TheDcoder
      Hello everyone, it has been a while since I have been involved in a serious AutoIt-related project discussion in the forums here, so it feels good to be back at it
      First a little backstory, a few months back (or a year ago) I made the switch to Linux from Windows... there were several reasons as to why I was motivated to do it, but the main things was Windows 10 was getting out of hand, it didn't offer enough freedom out of the box (restrictive settings, greedy defaults etc.) and had several issues with all kinds of software, many of them mysteriously hard to fix.
      I did have Linux installed as my secondary operating system for few years at that point and I began to see me regularly use it more than Windows, so one day I went ahead and said "fork it", I have stuck to Linux from that point on as my primary operating system.
      Windows is still installed in my computer and I also have a virtual machine with the latest version of Windows 10 on it, primarily because I still use Windows to write software using AutoIt as part of my job (freelance).
      I was pretty sad about the fact that I could no longer use AutoIt to make stuff for myself (since it doesn't run on Linux), I missed sharing code with the community here, this was a great hobby for me as AutoIt is what got me start with programming (even though I had tried several times to get into other languages before, those attempts were never as successful as what I have done in AutoIt).
      In Linux there is no language which is similar to AutoIt unfortunately, there are a few things which come close but are not on par with AutoIt's ease of use. As some of you may know, I have recently started working on other projects using the C language, it is a great step I have taken and I am learning a lot... but it is still not easy to create quick scripts or programs in C, due to the amount of configuration and dedication it takes to make a C program, also it is not ideal for sharing as multiple source files are involved. And on top of that you still have to deal with platform-specific APIs since C is a low-level language.
      (Getting back to core of this topic's subject)
      To solve this issue and to sharpen my programming skills, I could start working on an alternate cross-platform implementation of the AutoIt language which should be backwards compatible with the current AutoIt v3 on C
      The primary aim in the beginning is to implement all the basic functions (File, String, Numbers etc.)  in AutoIt so that it is possible to make basic and fundamental scripts which would work in both Windows and Linux
      The main features would be:
      Cross-platform (Linux and Windows with the possibility supporting of Mac) Backward compatible with AutoIt 3 Byte-code based interpreter instead of source interpreter Easy to use (just like the current AutoIt) The advantage of having a byte-code based interpreter is that it makes the interpreter lighter and faster since it doesn't have to deal with parsing the source code, this would also open the door for some compile time optimizations... another bonus thing for those who don't like their source code present inside the compiler binaries is that it should be impossible to get hands on the original source code, this of course does not mean that decompilation can be stopped... it merely means that anyone decompiling would have to reconstruct the code from the byte-code.
       
      We can later on implement GUI related functions and maybe even automation related functions if the project gets enough traction
      I am adding a poll to keep track of people who are willing to invest in this project or would like to see it come into existence. Welcome any comments, suggestions etc.
      Obviously I will have a lot to learn and my work might interfere with this at times, but with enough interest I can see myself working on this to deliver a very basic prototype which could be used with basic scripts.
       
×
×
  • Create New...