AutoHotkey Community

It is currently May 27th, 2012, 5:44 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Help needed in INI file
PostPosted: March 17th, 2010, 8:23 am 
Hi All..
I am trying to insert a key into my ini file.
in the beginning, my ini file looks like this:

[name]

[DOB]

[Addr]

I need to add in new keys into this ini file.
For example under section:

[name]
name 1= henry
name 2= John

I have tried using ini basic library. but do not know why it doesnt work.
below is my code:
Code:
Send ^c
ClipWait 
clipboard=%clipboard%
inifile=%A_scriptDir%\Particulars.ini
ini_load(no_ofkeys,inifile)
keys:= ini_getAllKeyNames(no_ofkeys,"name",a)
a:=a+1
namekey=name %a%
ini_insertKey(inifile, "name", namekey "=". clipboard)


in my code, i counted the number of keys in the section and store it in "a". there after i increase the value a by 1 so that it will look like this(from name 1 to name 2)

but when i run the program, seems like the key is not inserted into the ini file. Is there a problem with my code?

thanks for helping as I am quite a noob with AHK.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2010, 9:52 am 
Quote:
ini basic library
Would you mind to link to it. Might make sense if we're able to start our investigation there ... :)


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2010, 9:55 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Tuncays' lib http://www.autohotkey.com/forum/topic46226.html I'm sure he'll jump in when he sees this :D

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2010, 9:55 am 
hi.. This is the ini_insertkey function where i got it from one of the library in the forum.



Code:
ini_insertKey(ByRef _Content, _Section, _Key)
{
    StringLeft, K, _Key, % InStr(_Key, "=") - 1
    sectionCopy := ini_getSection(_Content, _Section)
    keyList := ini_getAllKeyNames(sectionCopy)
    isInserted = 0
    If K Not In %keyList%
    {
        sectionCopy .= "`n" . _Key
        isInserted = 1
    }
    If isInserted
    {
        ini_replaceSection(_Content, _Section, sectionCopy)
        ErrorLevel = 0
    }
    Else
    {
        ErrorLevel = 1
    }
    Return isInserted
}

Quote:
/*
Func: ini_insertKey
Adds a key pair with its name and value, if key does not already exists.

Parameters:
Content - *Variable* Content of an ini file (also this can be one
section only).
Section - Unique name of the section. (An enmpty string "" is not
working.)
Key - Key and value pair splitted by an equality sign.

Returns:
Returns 1 if key is inserted, and 0 otherwise (opposite of ErrorLevel).

Remarks:
Currently, it works as a workaround with different function calls instead
of one regex call. This makes it slower against the other functions.

Examples:
> ini_insertKey(ini, "Tip", "TimeNow=" . 20090925195317)
> value := ini_getValue(ini, "Tip", "TimeNow")
> MsgBox %value%
*Output:*
> 20090925195317
*/


thanks!


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2010, 11:06 am 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
First of all to say is, the ini file itself is not touched in anyway. Other than the AutoHotkey built-in commands, these ini_ functions works on the variables only. Reaso is superior speed if needed more than one operation, especially at loops etc, because the file is read and saved once.

And yes, you have to save the ini content back to file.

In example instead of
X
Code:
ini_insertKey(ini_filepath ...)

you should work on variable
O
Code:
ini_insertKey(ini_content ...)


Here is a working example:
Code:
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

inifile=%A_scriptDir%\Particulars.ini
GoSub, init
Return ; End of Autoexecute section

; Initialization. This is done once at startup.
init:
inifile := ini_load(inicontent,inifile)
If (ErrorLevel = 1)
{
    MsgBox, 48, File not found, The does not exist or was not loaded successfully. Script terminates.
    ExitApp
}
Else
{
    keys := ini_getAllKeyNames(inicontent,"name")
    ; Count only keys with name format "name1".
    ; It could have other keys, which should not be counted.
   
    Loop, Parse, keys, `,
    {
        If (A_LoopField = "name " . A_Index)
        {
            KeyCount++
        }
    }
}
Return

; Hotkey Ctrl+Alt+c copies text to clipboard and adds that content to ini file.
; Clipboard is restored.
^!c::
; Save clipboard before operation begins.
ClipBackup := ClipboardAll
Clipboard := ""
Send, ^c
ClipWait, 0.5, 1
If (ErrorLevel = 0)
{
    ; Continue only on success and restore original clipboard.
    textFromClip := Clipboard
    Clipboard := ClipBackup
    ; Process with adding the key.
    KeyCount++
    keyToAdd := "name " . KeyCount . "=". textFromClip
    MsgBox, 36, debug, Add key?`n----`n%keyToAdd%`n----
    IfMsgBox, Yes
    {
        ini_insertKey(inicontent, "name", keyToAdd)
        GoSub, SaveFile
    }
}
Else
{
    Clipboard := ClipBackup
}
Return

; Overwrites the file.
SaveFile:
FileDelete, %inifile%
FileAppend, %inicontent%, %inifile%
Return

Its not extensively tested, but it works. The Hotkey Ctrl+Alt+c adds the copied content to file. A MsgBox askes before that is done.

_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 18th, 2010, 4:41 am 
HI Tuncay,
thank you for your help, right now it works. but as of now I managed to insert key into the ini file. But I required my key to start at a new line each time. Is there a way for me to do it?

I managed to make use some of your codes from your previous post. Currently my code is as below:
Code:
saveclipboard=%clipboard%
inifile=%A_scriptDir%\Particulars.ini
inifile := ini_load(inicontent,inifile)
keys:= ini_getAllKeyNames(inicontent,"IC",a)
a:=a+1
keyToAdd := "IC " . a . "=". saveclipboard
ini_insertKey(inicontent, "IC", keyToAdd)
FileDelete, %inifile%
FileAppend, %inicontent%, %inifile%
clipboard=" "
winclose,A


So really need your help in inserting a new line. thanks!


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 18th, 2010, 10:12 am 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
Why inserting a new line? At what position? Are the keys you add all merged into one line?? Would you give please one example output, so I can understand this. Normally, the keys should be added in a new line. If you do want do that manually, Ahk provides a way with escaped characters.

`n is a new line, in example used at MsgBoxes. Windows text files have generally `r`n. In example:
Code:
keyToAdd := "`r`nIC " . a . "=". saveclipboard


More about that topic:
Note about Clipboard: You saved the clipboard with saveclipboard=%clipboard%, but %Clipboard% variable converts the data to text, and loose some formattings etc. The other built-in %ClipboardAll% contains all data without loosing anything. For your example it is ok, but after the work you free the clipboard. It would be better you recover the clipboard back before the script starts this work. In example with:

Code:
clipboard := saveclipboard

_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 18th, 2010, 11:14 am 
If the issue is to add a key eg at the 3rd section of a five section ini (that's what I think is this thread about), why not extract the whole section to delete/replace* it with the complete updated section using FileAppend :?:

* kindly recommended by Tuncay at his own INI thread: http://www.autohotkey.net/~majkinetor/Ini/Ini.html


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 18th, 2010, 11:29 am 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
@Murx
my ini lib can do this also :D
Code:
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir% 
ini =
(
[Tip]
TimeStamp = 20090716194758
[Recent File List]
File1=F:\testfile.ahk
File2=Z:\tempfile.tmp
)
MsgBox,, Before, %ini%
section := ini_getSection(ini, "Recent File List")
NewSection =
(
[Files]
Name1=Programs
Path1=C:\Program Files
)
ini_replaceSection(ini, "Recent File List", NewSection)
MsgBox,, After, %ini%

_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Apollo, Bing [Bot], Google Feedfetcher, mrhobbeys, rbrtryn and 57 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group