AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Help needed in INI file

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Vanessa
Guest





PostPosted: Wed Mar 17, 2010 7:23 am    Post subject: Help needed in INI file Reply with quote

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.
Back to top
Murx
Guest





PostPosted: Wed Mar 17, 2010 8:52 am    Post subject: Reply with quote

Quote:
ini basic library
Would you mind to link to it. Might make sense if we're able to start our investigation there ... Smile
Back to top
SoLong&Thx4AllTheFish



Joined: 27 May 2007
Posts: 4999

PostPosted: Wed Mar 17, 2010 8:55 am    Post subject: Reply with quote

Tuncays' lib http://www.autohotkey.com/forum/topic46226.html I'm sure he'll jump in when he sees this Very Happy
_________________
AHK Wiki FAQ
TF : Text files & strings lib, TF Forum
Back to top
View user's profile Send private message
Vanessa
Guest





PostPosted: Wed Mar 17, 2010 8:55 am    Post subject: Reply with quote

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!
Back to top
Tuncay



Joined: 07 Nov 2006
Posts: 1886
Location: Germany

PostPosted: Wed Mar 17, 2010 10:06 am    Post subject: Reply with quote

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! <--
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Vanessa
Guest





PostPosted: Thu Mar 18, 2010 3:41 am    Post subject: Reply with quote

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!
Back to top
Tuncay



Joined: 07 Nov 2006
Posts: 1886
Location: Germany

PostPosted: Thu Mar 18, 2010 9:12 am    Post subject: Reply with quote

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`n[color=blue]IC " . 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! <--
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Murx
Guest





PostPosted: Thu Mar 18, 2010 10:14 am    Post subject: Reply with quote

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 Question

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



Joined: 07 Nov 2006
Posts: 1886
Location: Germany

PostPosted: Thu Mar 18, 2010 10:29 am    Post subject: Reply with quote

@Murx
my ini lib can do this also Very Happy
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! <--
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group