| View previous topic :: View next topic |
| Author |
Message |
thomas Newbie
Joined: 15 Jan 2003 Posts: 23 Location: Germany
|
Posted: Wed Jan 21, 2004 11:45 am Post subject: delete ini file entry |
|
|
Hello,
I have a ini file like this:
[test]
test1=11223
test2=
Test3=5
Now I need to delete the parameter test2. So the follow ini are the result:
[test]
test1=11223
Test3=5
Does anybody know a solution with vds to do this?
Thanks!! |
|
| Back to top |
|
 |
CodeScript Moderator Team

Joined: 08 Jun 2003 Posts: 1060 Location: India
|
Posted: Wed Jan 21, 2004 3:57 pm Post subject: |
|
|
Hi thomas
If you are using VDS 5 try this one (at your own risk)
| Code: | #Define Function,WriteIniString
REM Syntax : @WriteIniString(<section name>,<key name to Delete>,<Ini File Name>)
%B = @WriteIniString(SectionOne,Test2,@PATH(%0)test.ini)
EXIT
REM You can include the code below in a seperate Unit:
:WriteIniString
LOADLIB KERNEL32
REM BOOL WritePrivateProfileString(
REM LPCTSTR lpAppName, // section name
REM LPCTSTR lpKeyName, // key name
REM LPCTSTR lpString, // string to add
REM LPCTSTR lpFileName // initialization file
REM );
%A = @LIB(KERNEL32,WritePrivateProfileStringA,int:,str:%1,str:%2,void:,str:%3)
FREELIB KERNEL32
EXIT %A |
Save the below as test.ini in the same folder as above to test the script.
| Code: |
[SectionOne]
Test1=this is test line1
Test2=this is test line2 |
Key Test2 should be deleted if successful. _________________ Regards
- CodeScript
Give your application a professional look with the VDSGUI Extension |
|
| Back to top |
|
 |
Skit3000 Admin Team

Joined: 11 May 2002 Posts: 2166 Location: The Netherlands
|
|
| Back to top |
|
 |
jules Professional Member


Joined: 14 Sep 2001 Posts: 1043 Location: Cumbria, UK
|
Posted: Wed Jan 21, 2004 4:02 pm Post subject: |
|
|
Windows does not, as far as I know, provide an API to do this.
Usually, it doesn't do any harm to leave an entry blank. If your use of INI files would leave a lot of blank entries, then the following routine should clean them up:
| Code: | :CleanINIFile
list create,31
list loadfile,31,%%inipath
if @ok()
%I = 0
while @greater(@count(31),%I)
%D = @item(31,%I)
%L = @len(%D)
if @equal(@substr(%D,%L),=)
list delete,31
else
%I = @succ(%I)
end
wend
list savefile,31,%%inipath
end
list close,31
exit |
Note that the default value entry in @iniread() is returned only when the value does not exist, not when it exists but is blank. I have tripped up a few times because of this... _________________ The Tech Pro
www.tech-pro.net |
|
| Back to top |
|
 |
PGWARE Web Host

Joined: 29 Dec 2001 Posts: 1565
|
Posted: Wed Jan 21, 2004 4:50 pm Post subject: |
|
|
You can load the inifile into a listbox, repeat through each item until the you find the @pos("test2=", LISTBOX) to be greater then 0, then simply delete the line from the listbox and save the file back to disk.
| Code: |
list create,1
list loadfile,1,INIFILE.INI
if @greater(@count(1), 0)
repeat
if @greater(@pos("test2=", @item(1, @succ(@index(1)))), 0)
list delete,1
end
until @equal(@succ(@index(1)), @count(1))
end
list savefile,1,INIFILE.INI
list close,1
|
|
|
| Back to top |
|
 |
Skit3000 Admin Team

Joined: 11 May 2002 Posts: 2166 Location: The Netherlands
|
|
| Back to top |
|
 |
FreezingFire Admin Team

Joined: 23 Jun 2002 Posts: 3508
|
Posted: Wed Jan 21, 2004 6:57 pm Post subject: |
|
|
You can also use "xini.dll"...
Download: http://www.vdsworld.com/index.php?page=download&fileid=16
Description:
| Quote: | | This extension allows the deletion of keys and entire sections in INI files. Also a command to return all section names or values within a section. |
_________________ FreezingFire
VDSWORLD.com
Site Admin Team |
|
| Back to top |
|
 |
Garrett Moderator Team
Joined: 04 Oct 2001 Posts: 2149 Location: A House
|
Posted: Wed Jan 21, 2004 8:36 pm Post subject: |
|
|
I usually just load the ini into a list, @match() the section name and then
the key name with the "=", and then delete that item in the list.
| Code: |
LIST CREATE,1
LIST LOADFILE,1,yourfile.ini
IF @text(1)
LIST SEEK,1,0
%A = @match([section],1)
IF %A
%A = @match(key=,1)
IF %A
LIST DELETE,1
LIST SAVEFILE,1,yourfile.ini
END
END
END
LIST CLEAR,1
LIST CLOSE,1 |
_________________ 'What you do not want done to yourself, do not do to others.' - Confucius (550 b.c. to 479 b.c.) |
|
| Back to top |
|
 |
Dr. Dread Professional Member


Joined: 03 Aug 2001 Posts: 1065 Location: Copenhagen, Denmark
|
Posted: Thu Jan 22, 2004 1:17 am Post subject: |
|
|
| Garrett wrote: | I usually just load the ini into a list, @match() the section name and then
the key name with the "=", and then delete that item in the list.
|
If you do this, you gotta be sure that the INI does not have the same key name in another section -
or else you might end up deleting the wrong key if the right section does not have a key by that name
any more.
Perhaps something like this:
| Code: |
%%inifile = yourfile.ini
%%section = [section]
%%keyname = yourkey
LIST CREATE,1
LIST LOADFILE,1,%%inifile
if @match(1,%%section)
%%increment = @next(1)
repeat
%%key = @item(1)
if @equal(@substr(%%key,1,4),%%keyname"=")
LIST DELETE,1
%%found = yes
LIST SAVEFILE,1,%%inifile
else
%%increment = @next(1)
end
until @equal(@pos("[",%%key),1)@equal(%%found,yes)
end
LIST CLOSE,1
|
BTW: I think you reversed your @match() parameters, Garrett
Greetz
Dr. Dread _________________ ~~ Alcohol and calculus don't mix... Don't drink and derive! ~~
String.DLL * advanced string processing |
|
| Back to top |
|
 |
CodeScript Moderator Team

Joined: 08 Jun 2003 Posts: 1060 Location: India
|
Posted: Thu Jan 22, 2004 3:47 am Post subject: |
|
|
| jules wrote: | | Windows does not, as far as I know, provide an API to do this. |
| msdn wrote: |
WritePrivateProfileString
lpString
[in] Pointer to a null-terminated string to be written to the file.
If this parameter is NULL, the key pointed to by the lpKeyName
parameter is deleted. |
I used the above API in my first reply to this post and here it
deletes the given key very well vds 5.01 on XP.
Does it not work with others OR No one is interested in trying
that at all
If definition of "solution with vds" does not include use of API then
that is a different thing.
BTW similar thing can be used to delete entire sections from ini file. _________________ Regards
- CodeScript
Give your application a professional look with the VDSGUI Extension |
|
| Back to top |
|
 |
Garrett Moderator Team
Joined: 04 Oct 2001 Posts: 2149 Location: A House
|
Posted: Thu Jan 22, 2004 5:13 am Post subject: |
|
|
| Dr. Dread wrote: | | Garrett wrote: | I usually just load the ini into a list, @match() the section name and then
the key name with the "=", and then delete that item in the list.
|
If you do this, you gotta be sure that the INI does not have the same key name in another section -
or else you might end up deleting the wrong key if the right section does not have a key by that name
any more.
BTW: I think you reversed your @match() parameters, Garrett
Greetz
Dr. Dread |
I never name two keys the same name myself. But that's just me. And
as for the reverse, I probably did, as I just typed that off the top of my
head.
-Garrett _________________ 'What you do not want done to yourself, do not do to others.' - Confucius (550 b.c. to 479 b.c.) |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|