Remove a section from an INI file

Nov 13, 2008
255
3
www.thedave.me
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?
 
May 20, 2008
3,515
4
Elkridge, MD, USA
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
 
Jun 3, 2008
130
3
Temecula, CA
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.
 

samintz

Scott Mintz
May 20, 2008
1,503
16
Solon, OH, USA
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" <neil@jpsoft.com>
11/13/2008 12:33 PM
Please respond to



To
Samintz@ra.rockwell.com
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
 
May 30, 2008
42
0
From: JP Software Forums [mailto:neil@jpsoft.com]
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
 
Jun 3, 2008
130
3
Temecula, CA
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
Thread starter Title Forum Replies Date
Dmitry L. Kobyakov How to? How to remove the record of the old version of the Take Command? Support 9
MikeBaas Edit/remove kbd shortcuts of Macro Recorder? Support 7
gworley rd (Remove Directory) gives interesting error: Support 2
x13 TPIPE /REPLACE - Remove line (including EOL) Support 3
C NE1 have BTM to remove %appdata\JP Software\ etc... Support 0
H DOS CMD Sort+Add Sequence+Remove Sequence number Support 12
vefatica Remove double-quotes with TPIPE Support 4
H Remove all previous activations Support 4
D Fixed Cut and paste in TC's file/folder view does not remove file Support 17
vefatica Must update remove my taskbar-pinned shortcut? Support 20
A How to? Remove last argument on command line Support 4
samintz How to? remove adjacent common chars Support 2
C Drag-and-drop from file-view may "remove" Support 1
S Setup - remove older versions? Support 4
C Can't remove leading quote from a string Support 2
A Cannot permanently remove Tabs toolbar using View menu - bug? Support 8
noahcoad Fixed Can't @iniread a section w quotes in the name Support 6
Joe Caverly Clearing a section of the screen works in CMD, not in TC Support 2
G Documentation on TCMD.INI Support 9
RChrismon How to? Copy TCMD.INI to New Version Support 7
vefatica Documentation INI file location? Support 2
D Custom ini-file is not read, by tcmd.exe cli Support 6
Alpengreis Fixed New INI directive "ANSIWin10" is invalid Support 2
Alpengreis TCMD.INI: The "super hidden" problem ... Support 6
D Fresh instance of TCMD does not use TCMD.INI Support 7
C Optimum Placement for TCMD.ini and others? Support 6
cgunhouse Documentation Locating the TCMD.INI File Support 9
D V19: toolbar /r "path\tctoolbar.ini" Support 1
B Fixed Error messages from TCMD.INI Support 2
C tcmd.ini not loading from program dir? Support 5
Detlef Fixed ParameterChar in TCMD.INI no longer working Support 35
Jay Sage Toolbar Update to INI File Not Working Support 10
Jay Sage How to Determine INI File Used by TCMD Support 6
Jay Sage Unable to Specify INI File Support 19
A How to? TCC/LE: How do you add "edit-mode=insert" to ini file? Support 9
D How to? Using a custom ini file with TCC from Take Command Support 7
cgunhouse TCMD.INI Issue Support 7
Fross TCC INI Support 2
Jay Sage Specified INI File Refuses to Run Support 3
T TCTOOLBAR /C & TCTOOLBAR /I should reload toolbar from TCMD.INI, right? Support 1
T TCC reading TCMD.INI causing "Invalid item name" warnings and pauses Support 2
D ini directives and @select example in Help Support 0
nickles Reload tcmd.ini from the command line Support 11
deleyd How to? Alias in TCMD.INI file? Support 2
J Extended directory search - cure by renaming tcmd.ini Support 10
A How to? How to enable UTF-8 output in tcmd.ini Support 9
C Env vars in tcmd.ini Support 12
C multi-INI file merging Support 1
S Location of tcmd.ini in x64 Support 4
M HistoryExclude .INI directive Support 2

Similar threads