Jump to content
Sign in to follow this  
neo van matix

I hate it... find out the lowest entry

Recommended Posts

neo van matix

$INI_File = "c:\programs.ini"
$PROGRAMS = IniReadSectionNames($INI_File)

; Declare $a, Startvalue
$a = 100

For $i = 1 To $PROGRAMS[0]
    
    for $p = 1 to $PROGRAMS[0]
        ; Read out the actual priority, compare with $a,... if $b < $a, set $a = $b.
        $b = IniRead($INI_File, $PROGRAMS[$p], "priority", "0")
        IF $b < $a Then $a = $b
    Next
    
        ; "Where $priority = $a"
    If IniRead($INI_File, $PROGRAMS[$i], "Installed", "0") = 0  AND IniRead($INI_File, $PROGRAMS[$i], "priority", $a) Then
        
        $path = IniRead($INI_File, $PROGRAMS[$i], "path", "")
        RunWait($path, @TempDir, @SW_MINIMIZE)
        IniWrite($INI_File, $PROGRAMS[$i], "Installed", "1")
        
            If IniRead($INI_FILE, $PROGRAMS[$i], "Shutdown", "0") = 1 Then
                sleep(5000)
                shutdown(6)
            EndIf
            
        EndIf
        
Next

Kk,.. my problem: I've a programs.ini:

[shutdown]
shutdown=0
[nero]
installed=0
path=\\xx\AutoIT\12 Nero\AU_Nero-6.6.0.14.exe
priority=1
[paintshop]
installed=0
path=\\xx\AutoIT\17 Paint Shop Pro 6\AU_Paintshop.exe
priority=2

The script above do the following: Read out the Sections, runs the path's of sections where installed = 0, set installed to 1 and restart.

Thats the way the script works for now... but i liked to expand the functionality:

Every Section in the ini has a priority-value (1 and 2). I want to start the program first with the lowest priority-id.

So, i declare $a = 100 on the header of the script. In the loop, i will check, if the read-out priority is < than $a. If yes, i want to set $a to the readout-priority. If the Script reads each section, i would gain on end the section with the lowest ID in $a.

But it doestn work... i know, loop in loop is lame.. ^^

Some Ideas ?


Attention! English noob ^^

Share this post


Link to post
Share on other sites
BitRot

Some Ideas ?

What about this :

1) Initialize $a to 100, and $p to ""

2) Loop thru all sections and :

3) Check if the section contains an "Installed" -key with the value "0". If not, continue with 2)

4) Get the priority-value. If none present, continue with 2) (eventually give a warning)

5) If the read priority-value is lower than the one in $a than store the priority-value in $a, and read

the "path" -key into $p. Continue with 2)

6) Now you either have an empty string in $p, or the path to the program that is not installed and has the lowest priority.

Hope that helps. :lmao:

Share this post


Link to post
Share on other sites
neo van matix

grml,..

Sry, i'm not able to get my script working...

$INI_File = "c:\programs.ini"
$PROGRAMS = IniReadSectionNames($INI_File)

$a = "100"
$p = ""

For $i = 1 To $PROGRAMS[0]
    
    for $x = 1 to $PROGRAMS[0]
        $b = IniRead($INI_File, $PROGRAMS[$x], "priority", "0")
        
        IF $b < $a Then
            $a = $b
            $p = IniRead($INI_File, $PROGRAMS[$x], "path", "")
        EndIf
    Next
    
    If IniRead($INI_File, $PROGRAMS[$i], "Installed", "0") = 0  Then
        
        RunWait($p, @TempDir, @SW_MINIMIZE)
        IniWrite($INI_File, $PROGRAMS[$i], "Installed", "1")
        
            If IniRead($INI_FILE, $PROGRAMS[$i], "Shutdown", "0") = 1 Then
                sleep(5000)
                shutdown(6)
            EndIf
            
        EndIf
        
Next

I tried it, but i know, that... i hate loops, ...

Could anybody help me?


Attention! English noob ^^

Share this post


Link to post
Share on other sites
BitRot

grml,..

Sry, i'm not able to get my script working...

<snip>

I tried it, but i know, that... i hate loops, ...

Nice try, but you didn't do what I said. :geek::lmao:

Look at this :

$INI_File = "c:\programs.ini"
$PROGRAMS = IniReadSectionNames($INI_File)

$a = "100"
$p = ""
For $i = 1 To $PROGRAMS[0]
    if IniRead($INI_File, $PROGRAMS[$i], "installed", "-1")=0 Then
        $h = IniRead($INI_File, $PROGRAMS[$i], "priority", "100")
        if $h<$a Then
            $a=$h
            $p=IniRead($INI_File, $PROGRAMS[$i], "path", "")
        EndIf
    EndIf
Next
if $h="" then
  ConsoleWrite("Nothing to do !"&@crlf)
else
  ConsoleWrite("Priority : "&$a&" - Executable : "&$p&@crlf)
endif
You see, just a single loop :ph34r:

Share this post


Link to post
Share on other sites
neo van matix

Heyho, thanks for your Code. Ya code works - but, theres another little problem.

Your code works, if i've a continuing priority, beginning at 1.

Example:

$INI_File = "c:\programs.ini"
$PROGRAMS = IniReadSectionNames($INI_File)

$a = "100"
$p = ""
For $i = 1 To $PROGRAMS[0]
    if IniRead($INI_File, $PROGRAMS[$i], "installed", "-1") = 0 Then
        $h = IniRead($INI_File, $PROGRAMS[$i], "priority", "100")
        if $h<$a Then
            $a=$h
            $p=IniRead($INI_File, $PROGRAMS[$i], "path", "")
        EndIf
    EndIf
Next
if $h="" then
  msgbox(0, "", "Nothing to do !")
else
  msgbox(0, "", "Priority : "&$a&" - Executable : "&$p)
endif

programs.ini

[shutdown]
shutdown=1

[msoffice2000]
installed=0
path=\\cdmil28\AutoIT\01 Office 2000\AU_Office2000.exe
priority=1

[msoffice2000sp3]
installed=0
path=\\cdmil28\AutoIT\01 Office 2000\AU_Office2000_SP3.exe
priority=2

[msoffice2000pp]
install=0
path=\\cdmil28\AutoIT\01 Office 2000\AU_Powerpoint_Viewer.exe
priority=3

If i run the script, i get a msgbox with "AU_Office2000". Now, if i have installed Office2000, i will set "installed" @ [msoffice2000] to "1".

Yet i will run the script again. The next section with the lowest priority would be "2" @ [msoffice2000sp3]. The Output:

"Priority: 100 - Executable: "

I cant get the script to work... i try it for 2 hours... bah :lmao:

Some Ideas, whats wrong ?


Attention! English noob ^^

Share this post


Link to post
Share on other sites
jinxter

as it seems....

it will run through the for/next section and return nothing, when it reaches the end it $h will be set and not "" and the if statement will show the $p in the section last read.

what exactly do you want it to do ? run the programs in priority order if they're not installed ?


> there are 10 types of people in the world, those who understand binary and those who don't.

Share this post


Link to post
Share on other sites
neo van matix

Yeah, right - the script should start the installation with the lowest priority.


Attention! English noob ^^

Share this post


Link to post
Share on other sites
SmOke_N

#include <array.au3>;Only using this for _ArrayDisplay()
$b = _IniGetPriority(@DesktopDir & '\test.ini')
_ArrayDisplay($b, 'Returned Priority')
Func _IniGetPriority($hIni)
    Local $sTemp, $aTemp, $nHigh = -1
    Local $aNames = IniReadSectionNames($hIni)
    For $iCC = 1 To UBound($aNames)-1
        Local $sInstalled = IniRead($hIni, $aNames[$iCC], 'installed', 'NF')
        If $sInstalled <> 'NF' And Int($sInstalled) = 0 Then
            Local $sPriority = IniRead($hIni, $aNames[$iCC], 'priority', 'NF')
            If $sPriority <> 'NF' And Int($sPriority) > $nHigh Then
                $nHigh = Int($sPriority)
                $sTemp = $aNames[$iCC]
                $aTemp = $sTemp & Chr(1) & $aTemp
            ElseIf Int($sPriority) < $nHigh Then
                $sTemp = $aNames[$iCC]
                $aTemp &= $sTemp & Chr(1)
            EndIf
        EndIf
    Next
    If $aTemp Then Return StringSplit(StringTrimRight($aTemp, 1), Chr(1))
    Return SetError(1, 0, 0)
EndFunc

Edit:

Yeah, right - the script should start the installation with the lowest priority.

I miss understood, I thought you wanted to start with the "highest" priority. Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
BitRot

I cant get the script to work... i try it for 2 hours... bah :ph34r:

Some Ideas, whats wrong ?

Yes, I (after some testing) do : which string is, according to AI lower than the other : "2" or "100" ?

The answer ? "100" is lower than "2", as when Ai compares the strings it determines that "1" is lower than "2" (left-most digit of both numbers). :):geek:

Solution ? Make sure that "$a" and "$h" both are values instead of strings. Like this :

$INI_File = "c:\programs.ini"
$PROGRAMS = IniReadSectionNames($INI_File)

$a = 100   ;<-- Here
$p = ""
For $i = 1 To $PROGRAMS[0]
    if IniRead($INI_File, $PROGRAMS[$i], "installed", -1) = 0 Then   ;<-- Here
        $h = IniRead($INI_File, $PROGRAMS[$i], "priority", 100)   ;<-- And here
        if $h<$a Then
            $a=$h
            $p=IniRead($INI_File, $PROGRAMS[$i], "path", "")
        EndIf
    EndIf
Next
if $p="" then  ;<-- another small change here
  msgbox(0, "", "Nothing to do !")
else
  msgbox(0, "", "Priority : "&$a&" - Executable : "&$p)
endif
Its my bad : I assumed AI would convert the read string-data into values, and did not think further. :lmao:

Hope that helps.

Share this post


Link to post
Share on other sites
SmOke_N

I haven't seen what BitRot did, and if his works, then it's a much simpler concept then what I was thinking... Of course I've been up all night, so this could probably be done easier:

#include <array.au3>;Only using this for _ArrayDisplay()
$b = _IniGetPriority(@DesktopDir & '\test.ini');Decending
_ArrayDisplay($b, 'Returned Priority')

$b = _IniGetPriority(@DesktopDir & '\test.ini', 0);Ascending
_ArrayDisplay($b, 'Returned Priority')


Func _IniGetPriority($hIni, $iDecend = 1)
    Local $aNames = IniReadSectionNames($hIni), $nCC, $nTemp
    If Not IsArray($aNames) Then Return SetError(1, 0, 0)
    For $iCC = 1 To UBound($aNames)-1
        $nCC = $iCC
        If $iDecend Then
            For $xCC = $nCC To UBound($aNames) - 1
                If Int(IniRead($hIni, $aNames[$nCC], 'priority', 'NF')) >= _
                    Int(IniRead($hIni, $aNames[$xCC], 'priority', 'NF')) Then $nCC = $xCC
            Next
        Else
            For $xCC = $nCC To UBound($aNames) - 1
                If Int(IniRead($hIni, $aNames[$nCC], 'priority', 'NF')) <= _
                    Int(IniRead($hIni, $aNames[$xCC], 'priority', 'NF')) Then $nCC = $xCC
            Next
        EndIf
        $nTemp = $aNames[$iCC]
        $aNames[$iCC] = $aNames[$nCC]
        $aNames[$nCC] = $nTemp
    Next
    Local $sHold
    For $iCC = 1 To UBound($aNames) - 1
        If IniRead($hIni, $aNames[$iCC], 'installed', 'NF') <> 'NF' And _
            IniRead($hIni, $aNames[$iCC], 'priority', 'NF') <> 'NF' Then
            $sHold &= $aNames[$iCC] & Chr(1)
        EndIf
    Next
    Return StringSplit(StringTrimRight($sHold, 1), Chr(1))
EndFunc

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
neo van matix

Night? Hehe, i've 1:30 pm... *gg*

Thanks, u both - i tried the solution of BitRot first, and it works nearly correct. I've to write the priority in the ini's with a "0" in front of the number (0-9) - then it works.

Will try Smoke_N's solution too.

Much thanks for help!


Attention! English noob ^^

Share this post


Link to post
Share on other sites
SmOke_N

Night? Hehe, i've 1:30 pm... *gg*

Thanks, u both - i tried the solution of BitRot first, and it works nearly correct. I've to write the priority in the ini's with a "0" in front of the number (0-9) - then it works.

Will try Smoke_N's solution too.

Much thanks for help!

I know mine works, tested it a few times.

Edit:

This one is 30 to 70x's faster than my last:

Func _IniGetPriority($hIni, $iDecend = 1)
    Local $aNames = IniReadSectionNames($hIni)
    Local $nCC, $nTemp, $nNT
    If Not IsArray($aNames) Then Return SetError(1, 0, 0)
    Local $nNum, $sName
    For $iCC = 1 To UBound($aNames) - 1
        Local $vIns = IniRead($hIni, $aNames[$iCC], 'installed', 'NF')
        Local $vPri = IniRead($hIni, $aNames[$iCC], 'priority', 'NF')
        If $vIns <> 'NF' And $vPri <> 'NF' Then
            $sName &= $aNames[$iCC] & Chr(1)
            $nNum &= $vPri & Chr(1)
        EndIf
    Next
    $sName = StringSplit(StringTrimRight($sName, 1), Chr(1))
    $nNum = StringSplit(StringTrimRight($nNum, 1), Chr(1))
    For $iCC = 1 To UBound($sName)-1
        $nCC = $iCC
        If $iDecend Then
            For $xCC = $nCC To UBound($sName) - 1
                If Int($nNum[$nCC]) > Int($nNum[$xCC]) Then $nCC = $xCC
            Next
        Else
            For $xCC = $nCC To UBound($sName) - 1
                If Int($nNum[$nCC]) < Int($nNum[$xCC]) Then $nCC = $xCC
            Next
        EndIf
        $nTemp = $sName[$iCC]
        $nNT = $nNum[$iCC]
        $sName[$iCC] = $sName[$nCC]
        $nNum[$iCC] = $nNum[$nCC]
        $sName[$nCC] = $nTemp
        $nNum[$nCC] = $nNT
    Next
    Return $sName
EndFunc
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
BitRot

Thanks, u both - i tried the solution of BitRot first, and it works nearly correct. I've to write the priority in the ini's with a "0" in front of the number (0-9) - then it works.

Strange : when I use the last code I've posted it seems to work allright, without a need to alter the data in the INI-file.

Share this post


Link to post
Share on other sites
SmOke_N

Strange : when I use the last code I've posted it seems to work allright, without a need to alter the data in the INI-file.

Hmm, was he only looking for the first one that should be ran, or all of them in order of priority?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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  

×