 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Vanessa Guest
|
Posted: Wed Mar 17, 2010 7:23 am Post subject: Help needed in INI file |
|
|
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
|
Posted: Wed Mar 17, 2010 8:52 am Post subject: |
|
|
Would you mind to link to it. Might make sense if we're able to start our investigation there ...  |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
|
| Back to top |
|
 |
Vanessa Guest
|
Posted: Wed Mar 17, 2010 8:55 am Post subject: |
|
|
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
|
Posted: Wed Mar 17, 2010 10:06 am Post subject: |
|
|
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 |
|
 |
Vanessa Guest
|
Posted: Thu Mar 18, 2010 3:41 am Post subject: |
|
|
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
|
Posted: Thu Mar 18, 2010 9:12 am Post subject: |
|
|
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 |
|
 |
Murx Guest
|
Posted: Thu Mar 18, 2010 10:14 am Post subject: |
|
|
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 |
|
| Back to top |
|
 |
Tuncay
Joined: 07 Nov 2006 Posts: 1886 Location: Germany
|
Posted: Thu Mar 18, 2010 10:29 am Post subject: |
|
|
@Murx
my ini lib can do this also
| 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 |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|