Jump to content

Recommended Posts

Posted

Hi,

I have an ini file like the one below

How can I delete keys that have no value in this file? (08:30 and 08:50 in the example)

Thanks

example ini

[MONDAY]
08:00=example1
08:20=example2
08:30=
08:40=example3
08:50=example4
08:50=

 

Posted (edited)

@ahmetpi, btw, your example has duplicate keys:

08:50=example4
08:50=

This should never happen if using only the INI functions.  I assume it’s just because of a hastily thrown together example, but thought I would point it out in case it’s not.

Edited by JockoDundee

Code hard, but don’t hard code...

Posted
19 minutes ago, JockoDundee said:

This should never happen if using only the INI functions.

Example()

Func Example()
    ; Create a constant variable in Local scope of the filepath that will be read/written to.
    Local Const $sFilePath = "Test.ini"

    ; Create an INI section structure as an array. The zeroth element is how many items are in the array, in this case 3.
    Local $aSection[4][2] = [[3, ""], ["Test", "AutoIt"], ["Test", @AutoItVersion], ["Test", @OSVersion]]

    ; Write the array to the section labelled 'General'.
    IniWriteSection($sFilePath, "General", $aSection)

EndFunc   ;==>Example

:ermm:

Posted
1 hour ago, Nine said:
Example()

Func Example()
    ; Create a constant variable in Local scope of the filepath that will be read/written to.
    Local Const $sFilePath = "Test.ini"

    ; Create an INI section structure as an array. The zeroth element is how many items are in the array, in this case 3.
    Local $aSection[4][2] = [[3, ""], ["Test", "AutoIt"], ["Test", @AutoItVersion], ["Test", @OSVersion]]

    ; Write the array to the section labelled 'General'.
    IniWriteSection($sFilePath, "General", $aSection)

EndFunc   ;==>Example

:ermm:

I stand corrected, its absolutely possible. Thanks.

 

 

 

 

Code hard, but don’t hard code...

Posted
13 hours ago, JockoDundee said:

@ahmetpi, btw, your example has duplicate keys:

08:50=example4
08:50=

This should never happen if using only the INI functions.  I assume it’s just because of a hastily thrown together example, but thought I would point it out in case it’s not.

I wrote it twice by mistake :)
sorry

  • 5 weeks later...
Posted

@JockoDundee

Duplicate entries are possible, but not recommended and can lead to unpredictable results.

There are programs, that will take for duplicate keys the first value when reading the ini, others will take the last one.

And Autoit is behaving differnt for "duplicates already exist" and "no duplicates so far". Basically: The results are hardly predictable, and duplicates for most purposes should be avoided IMHO.

 

Example()

Func Example()
    ; Create a constant variable in Local scope of the filepath that will be read/written to.
    Local Const $sFilePath = "Test.ini"

    ; Create an INI section structure as an array. The zeroth element is how many items are in the array, in this case 3.
    Local $aSection[4][2] = [[3, ""], ["Test", "AutoIt"], ["Test", @AutoItVersion], ["Test", @OSVersion]]

    ; Write the array to the section labelled 'General'.
    IniWriteSection($sFilePath, "General", $aSection)
    ShellExecute($sFilePath)
    MsgBox(0, '', "waiting")

    for $i = 1 to $aSection[0][0]
        IniWrite($sFilePath,"General",$aSection[$i][0],$aSection[$i][1])
    Next
    ShellExecute($sFilePath)
    MsgBox(0, '', "Written ""over"" results in just the first occurence is written to three times")

    IniDelete($sFilePath,"General")
    for $i = 1 to $aSection[0][0]
        IniWrite($sFilePath,"General",$aSection[$i][0],$aSection[$i][1])
    Next
    ShellExecute($sFilePath)

EndFunc   ;==>Example

 

 

 

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Posted
2 hours ago, rudi said:

Duplicate entries are possible, but not recommended and can lead to unpredictable results.

Yes, I agree.  I would have said as much earlier, but I was so mortified by my inaccurate statement that I thought it best just to admit I was wrong, instead of seeming to say “Yes, but...” :)

Code hard, but don’t hard code...

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...