Jump to content

Getting Last Section From .ini


Darkz0r
 Share

Recommended Posts

; Write version.ini on ScriptDir for testing purposes
FileDelete(@ScriptDir & "\version.ini")
IniWrite(@ScriptDir & "\version.ini", "1.0", "URL", "http://www.update.com/1.0")
; __________________________________________________________________
IniWrite(@ScriptDir & "\version.ini", "1.1", "URL", "http://www.update.com/1.1")
; __________________________________________________________________
IniWrite(@ScriptDir & "\version.ini", "1.2", "URL", "http://www.update.com/1.2")
; __________________________________________________________________



; Declare the current version of the Application
Local $client_version = "1.0" ; In my actual script, I use FileRead with StringRegExp

; Read the section names
Local $SectionNames = IniReadSectionNames(@ScriptDir & "\version.ini")
For $a = 1 To $SectionNames[0] ; Creating a loop to get all section names
    ; Now we read the keys of each Section
    Local $keys = IniReadSection(@ScriptDir & "\version.ini", $SectionNames[$a])
    ; If an error occurred when trying to Read
    If @error Then
        MsgBox(4096, "", "Error occurred, probably no INI file.")
    ; If no error
    Else
        ; Creates a loop to get all keys within each section
        For $i = 1 To $keys[0][0]
            ; Now show us each section, key and their values
            MsgBox(4096, "", "Section: " & $SectionNames[$a] & @CRLF _ ; Break line
            & "Key: " & $keys[$i][0] & @CRLF & "Value: " & $keys[$i][1])
        Next
    EndIf
Next

So, I'm using "IniReadSectionNames" as you can see to get the sections but how can I get the last section? (Without actually have to set it manually?)

I've tried some method already I've been reading, but the more I read, the more confused I get...

Any help?

Edited by Darkz0r
Link to comment
Share on other sites

To read the last section of the returned section list use something like this:

$Keys = IniReadSection(@ScriptDir & "\version.ini", $SectionNames[$SectionNames[0]])

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

IniReadSectionNames - The number of elements returned will be in $result[0]. That should answer your question.

Not really, well let me explain. This script is for an autoupdater project i'm working on, so i'll keep updating the .ini with new versions.

There are 3 version currently in that script yes, but that was an example. I need this script to automatically stay up-to-date with the last version, so if I add a newer version (in this case 1.4), I don't have to edit the script to set $result[<number here>] everytime.

To read the last section of the returned section list use something like this:

$Keys = IniReadSection(@ScriptDir & "\version.ini", $SectionNames[$SectionNames[0]])

That is not really working... :(
Link to comment
Share on other sites

If I understand you correctly: You don't want the "last" section you want the section with the "highest" number.

As the section names are strings you need to make sure that all elements of the version number are in the same format. Means: "2.1.1.8" has to become "02.01.01.08" so you can sort the array of section names and then grab the last = highest one.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

That's exactly what I want. A simple word changes it all hehe :P

Ok I got it,

_ArrayMax($SectionNames, 0, 1)

did the trick but there's no workaround for having to set them in same format? Because I need to compare the returned value of the _ArrayMax with the ones I read from a external file... and if they're in different formats that'll be hard...

Edited by Darkz0r
Link to comment
Share on other sites

You can use something like that to norm the version numbers:

$sVersionNumber = "1.2.03.4"
$aNumbers = StringSplit($sVersionNumber, ".")
$sFormatedVersionNumber = StringFormat("%.2d.%.2d.%.2d.%.2d", $aNumbers[1], $aNumbers[2], $aNumbers[3], $aNumbers[4])
MsgBox(0, "", $sFormatedVersionNumber)

Or you can compare the parts of the version number mathematical... Then "01" is "1". :P

chess

Edited by chesstiger
Link to comment
Share on other sites

I have another question, but I'll use this thread instead of creating another one, I don't think it's really necessary...

What if I want to find out the next value in the array?

I'm sure I need to use a loop but I'm not getting exactly how...

Here's my current code:

#include <Array.au3>

$Read_ClientVersion = "2.1.1.8"
$aNumbers = StringSplit($Read_ClientVersion[0], ".")
$Client_Version = StringFormat("%.2d.%.2d.%.2d.%.2d", $aNumbers[1], $aNumbers[2], $aNumbers[3], $aNumbers[4])

; Write version.ini on ScriptDir for testing purposes
FileDelete(@ScriptDir & "\version.ini")
IniWrite(@ScriptDir & "\version.ini", "02.01.01.08", "URL", "http://www.update.com/1.0.rar")
; __________________________________________________________________________________
IniWrite(@ScriptDir & "\version.ini", "02.01.01.09", "URL", "http://www.update.com/1.1.rar")
; __________________________________________________________________________________
IniWrite(@ScriptDir & "\version.ini", "02.01.01.10", "URL", "http://www.update.com/1.2.rar")
; __________________________________________________________________________________

; Read the section names
Local $SectionNames = IniReadSectionNames(@ScriptDir & "\version.ini")
; Get the last section (last version)
Local $last_section = _ArrayMax($SectionNames, 0, 1)

If $client_version = $last_section Then
MsgBox("", "Notice", "Your version is up-to-date.")
Else
MsgBox("", "Notice", "Your version is out-of-date.")
EndIf


For $a = 1 To $SectionNames[0] ; Creating a loop to get all section names
; Now we read the keys of each Section
Local $keys = IniReadSection(@ScriptDir & "\version.ini", $SectionNames[$a])
; If an error occurred when trying to Read
If @error Then
MsgBox(4096, "", "Error occurred, probably no INI file.")
; If no error
Else
; Here we will get the keys, with the URL for updating.
EndIf
Next
Edited by Darkz0r
Link to comment
Share on other sites

You first want to get the last entry and then you want to get the next entry after the last entry?! Is this correct?

Edited by guinness

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

My point was there is no "next one" after the last one, do you mean the one before that?

Why not look at the For loop section in the help file and Step. You will want to step backwards through the array so will need to use Step -1. Ammend the code and let us know if you get stuck.

Trust me this is how you learn.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

I'm sorry if I made you misunderstood my question.

That's not at all what I need. As you can see above in my code I have this variable that was manually defined by me: $Read_ClientVersion = "2.1.1.8".

Using _ArrayMax I got the variable $last_section to hold the last version of my app (which in this case is 2.1.1.10).

Then I compare if $Read_ClientVersion and $last_section hold the same values, if yes good, the application is up-to-date, if not, then it'll find the next Section (version) above 2.1.1.8 (which in my case is 2.1.1.9).

Why this? Because with each update the filesize of the update package would get bigger, so a user with version 2.1.1.9 would have to download the whole package (meant to all users) just to update to 2.1.1.10. So with this method, users will download only what they need, 2.1.1.9 users will just download 2.1.1.9-2.1.1.10 patch.

Hope this helped clarify my idea, but if not, take a look here please:

It's not like I didn't already, but having no luck... :(

Link to comment
Share on other sites

It's easier if I just give you the code than trying to explain it. Use the help file to get an idea of what is happening.

#include <Array.au3>

$Read_ClientVersion = '2.1.1.8'
$aNumbers = StringSplit($Read_ClientVersion, '.')
$Client_Version = StringFormat('%.2d.%.2d.%.2d.%.2d', $aNumbers[1], $aNumbers[2], $aNumbers[3], $aNumbers[4])

; Write Version.ini on ScriptDir for testing purposes
FileDelete(@ScriptDir & '\Version.ini')
IniWrite(@ScriptDir & '\Version.ini', '02.01.01.08', 'URL', 'http://www.update.com/1.0.rar')
IniWrite(@ScriptDir & '\Version.ini', '02.01.01.09', 'URL', 'http://www.update.com/1.1.rar')
IniWrite(@ScriptDir & '\Version.ini', '02.01.01.10', 'URL', 'http://www.update.com/1.2.rar')

; Read the section names
Local $aSectionNames = IniReadSectionNames(@ScriptDir & '\Version.ini')
; Get the last section (last Version)
Local $Last_Section = _ArrayMax($aSectionNames, 0, 1)

If $Client_Version == $Last_Section Then
    MsgBox('', 'Notice', 'Your Version is up-to-date.')
Else
    MsgBox('', 'Notice', 'Your Version is out-of-date.')
EndIf

Local $fFound = False, $sURLsHold = ''
For $i = 1 To $aSectionNames[0]
    If $Client_Version == $aSectionNames[$i] Then
        $fFound = True
        ContinueLoop
    EndIf
    If $fFound = False Then
        ContinueLoop
    EndIf
    $sURLsHold &= IniRead(@ScriptDir & '\Version.ini', $aSectionNames[$i], 'URL', '') & @LF
Next
$sURLsHold = StringStripWS($sURLsHold, 3)
Local $aURLs = StringSplit($sURLsHold, @LF)
_ArrayDisplay($aURLs)

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

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