Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Remove a section from an INI file

Nov
257
3
Howdy!

I'm looking for a way to remove an entire section from an INI file including the header, not just the individual keys and settings contained within, can anyone tell me if that's possible using internal commands or do I need to parse the INI file myself?
 
thedave wrote:
|
| I'm looking for a way to remove an entire section from an INI file
| including the header, not just the individual keys and settings
| contained within, can anyone tell me if that's possible using
| internal commands or do I need to parse the INI file myself?

Sorry, you must do it yourself. The special functions for accessing INI file
content, @INIREAD and @INIWRITE are very limited. What you need is an
@INIDELETE function, which could be built as a plug-in, but which nobody has
created.
--
Steve
 
In case anyone decides to try to write a plug-in, it should be REALLY easy. The standard windows API call, WritePrivateProfileString, can be used to delete the section in one call, providing NULL for the lpKeyName.

See http://msdn.microsoft.com/en-us/library/ms725501(VS.85).aspx for details.

Note that a very simple enhancement to the @IniWrite[] function would be to follow the same functionality as WritePrivateProfileString follows, namely, that

%@iniwrite[.\MyIni.ini,SectionToDelete,,]

would delete the section.
 
This batch file should do the trick:

if %1.==. .or. %2.==. (echo usage: %0 ini_name section_name & quit)
setlocal
set found_section=0
copy %1 %1.bak
del /q %1
set h=%@fileopen[%1,w]
:: turn off special character handling like pipes and redirection
setdos /x-456
:: iterate through every line in the original INI file
do l in @%1.bak
iff %found_section then
:: section names always begin with a [.
:: That's how we recognize the end of
:: the section (by the start of the next).
iff "%@left[1,%l]" == "[" then
set found_section=0
else
::skip over the lines in this section
iterate
endiff
endiff
:: If we find the section name then turn on
:: the flag that tells us to not copy those lines
iff %l == [%2] then
set found_section=1
iterate
endiff
:: otherwise, copy all the lines before and
:: after the section
set r=%@filewrite[%h,%l]
enddo
set r=%@fileclose[%h]
endlocal

-Scott




"JP Software Forums" <[email protected]>
11/13/2008 12:33 PM
Please respond to



To
[email protected]
cc

Subject
RE: [Support-t-638] Remove a section from an INI file






thedave wrote:
|
| I'm looking for a way to remove an entire section from an INI file
| including the header, not just the individual keys and settings
| contained within, can anyone tell me if that's possible using
| internal commands or do I need to parse the INI file myself?

Sorry, you must do it yourself. The special functions for accessing INI
file
content, @INIREAD and @INIWRITE are very limited. What you need is an
@INIDELETE function, which could be built as a plug-in, but which nobody
has
created.
--
Steve
 
From: JP Software Forums [mailto:[email protected]]
Sent: Thursday, November 13, 2008 11:53 AM
Subject: RE: [Support-t-638] Re: Remove a section from an INI file

>
> In case anyone decides to try to write a plug-in, it should be REALLY
> easy. The standard windows API call, WritePrivateProfileString, can be
> used to delete the section in one call, providing NULL for the lpKeyName.

A plugin is not necessary for a simple API call :-)

set
result=%@WINAPI[kernel32,WritePrivateProfileStringW,"SectionName",NULL,NULL,
"filename.ini"]

Done and done, modulo wrapping :-)

Jonathan Gilbert
 
Nice! Of course, since I'm only at 4nt 6.01, I didn't know/remember about the @WINAPI function. I'll still claim credit for the idea, even if you get credit for the implementation (as simple as it was). ;)
 

Similar threads

Back
Top