Jump to content
Sign in to follow this  
Blip

Write new line in existing .ini section

Recommended Posts

Blip

I'm trying to create a function that adds an additional key and value to an existing .ini section. The existing ini would look something like:

[Programs]
item=Program
item=Application
item=Exe

I have a simple GUI with a field and a button. The button triggers a function which I would like to add the input from the field into a new line under the [Programs] section in the existing ini. So if were to type in "NewValue" into the field and press the button, it should make the existing ini look like:

[Programs]
item=Program
item=Application
item=Exe
item=NewValue

The problem is, iniWrite seems to just overwrite the first existing occurance of the item value making the ini look like:

[Programs]
item=NewValue
item=Application
item=Exe

The iniSectionWrite seems to just overwrite the existing section entirely leaving this:

[Programs]
item=NewValue

I'm probably over looking some better function to use, and I'd like to avoid having to capture an array of all the existing keys and rewriting them back into a new section with iniSectionWrite. Ideally, I'd like to add that new line even if the existing key and value combo already exists under that section. In theory, if I pushed the button 5 times with the same value in the field, it would add 5 identical lines to that section. I'd appreciate any help.

Share this post


Link to post
Share on other sites
Blip

IniWrite does not support multiple keys with the same name, so the thing you want to avoid is the the only thing you can do. I had to do it in one of my scripts too.

I guess I'll have to go that route then, thanks. I'm still open to other suggestions if they're out there though. Thanks again.

Share this post


Link to post
Share on other sites
careca

You could add the key, and then rename the section to the number of times the key was pressed

IniWrite("C:\Temp\myfile.ini", "3", "Z", "Pressed")

[3]
Z=Pressed

or

[Z]
3=Pressed

You know?


Spoiler

Paster - Main function is to paste text, but has more functions. (No longer mantained, switched to String Trigger)

OpenW - Open With... alternative, Open any file with any application, set it's icon, set application as default.

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Firefox Profile Backup - Backup/restore previously saved profile.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Share this post


Link to post
Share on other sites
Blip

I found a semi-workaround because I embarrassingly was having trouble adding the new entry I wanted to the iniReadSection array.

I captured the existing entries with iniReadSection... Deleted the section with IniDelete... Recreated the section name with FileWrite, added the new Entry with FileWrite, then dumped the captured array underneath it with FileWrite.

$sIni = "Scriptsmenu.ini"
$Sec = InputBox("Section to edit", "Section:")
$New = InputBox("Entry to add", "Entry:")
Local $var = IniReadSection($sIni, $Sec)
IniDelete($sIni, $Sec)
Local $file = FileOpen($sIni, 1)
FileWrite($file, "[" & $Sec & "]" & @CRLF)
FileWrite($file, "Item=" & $New & @CRLF)
For $i = 1 To $var[0][0]
FileWrite($file, $var[$i][0] & "=" & $var[$i][1] & @CRLF)
Next
FileClose($file)

I'm still pretty bad working with arrays. I can create them from scratch, or pull them from something like iniReadSection. I run into serious trouble when I try to tinker with adding something to an array after it's been created.

This solution will do for now, but if anyone can easily explain how I would add an extra value to that iniReadSection array I'd love to know how to do it in the future. This would also probably make alphabetizing the section much easier too (I'm guessing?).

Share this post


Link to post
Share on other sites
kylomas

Blip,

When you try to read the section with a key of item what do you expect to get?

If you can live with a construct like this

[Programs]
item01=Program
item02=Application
item03=Exe

then it is just a mater of reading the section, finding the highest numbered entry (ubound of the returned array) and creating a new item with the key being "item" concatenated with the ubound of the returned array + 1.

kylomas

edit: add example

This is rough and no error checking but demonstrates the principle

#include <GUIConstantsEx.au3>

#AutoIt3Wrapper_Add_Constants=n

;----------------------------------------------------------------------------------------
; Create a test INI file
;----------------------------------------------------------------------------------------

local $inifile = @scriptdir & '\test.ini', $str

if not fileexists($inifile) then

    for $1 = 1 to 10
        for $2 = 1 to 5
            $str &= chr(random(65,90,1))
        Next
        iniwrite($inifile,"Programs","Item" & stringformat('%02s',$1),$str)
        $str = ''
    Next

endif

;----------------------------------------------------------------------------------------
; ADD INI Entry
;----------------------------------------------------------------------------------------

local $gui010   =   guicreate('Add an Entry to the Programs Section of my INI File')
local $inp010   =   guictrlcreateinput('Type Program Name Here',50,50,200,20)
local $btn010   =   guictrlcreatebutton('Display INI File',50,150,200,20)
                    guisetstate()

while 1
    switch guigetmsg()
        case $gui_event_close
            Exit
        Case $inp010
            _add(guictrlread($inp010))
        Case $btn010
            shellexecute($inifile)
    EndSwitch
WEnd

func _add($pgm)

    if stringlen(stringstripws($pgm,3)) = 0 then return

    local $aPrograms = IniReadSection($inifile,"Programs")
    local $EntryKey = "Item" & stringformat('%02s',$aPrograms[0][0] + 1)
    iniwrite($inifile,"Programs",$EntryKey,$pgm)

endfunc

edit2: Note - this only works if all entries are present (not deleted). If an entry is deleted manually then that slot may be overwritten.

Edited by kylomas
  • Like 1

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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  

×