Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Delete INI File Sections

Posted on 1999-07-09
11
Medium Priority
?
649 Views
Last Modified: 2008-02-26
How do you Delete a section of an INI file.

EX: win.ini

[windows]
load=
run=
NullPort=None
device=HP LaserJet 4Si MX,HPPCL5MS,\\ALVA-AMCO_NT01\HP8100DN

[Desktop]
Wallpaper=C:\WINDOWS\INTERN~1.BMP
TileWallpaper=1
WallpaperStyle=0

[FontSubstitutes]
Helv=MS Sans Serif
Tms Rmn=MS Serif
Times=Times New Roman
Helvetica=Arial
MS Shell Dlg=MS Sans Serif
MT Symbol=Symbol

How can I just delete the entire [Desktop] section?
0
Comment
Question by:wileecoy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
11 Comments
 
LVL 10

Expert Comment

by:caraf_g
ID: 1523122
You could just process it as a text file?
0
 
LVL 4

Author Comment

by:wileecoy
ID: 1523123
I realize that is an option - I am looking to see if there is an API that is consistent with the other INI API calls.
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1523124
I looked at the APIs and I can find read and write APIs for the section but I don't see anything to delete a section (or a value for that matter).
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Expert Comment

by:MrOBrian
ID: 1523125
As I recall, you use the WritePrivateProfileLong API call and write a &0...  I am pulling this out of memory, I don't think I have an example anywhere to make sure.  Try it, let me know if it works
0
 
LVL 2

Accepted Solution

by:
Torus earned 200 total points
ID: 1523126
use WritePrivateProfileSection

Public Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long


call writePrivateProfileSection("Desktop", "", "YourINI.ini")

It will detect all the keys in the section.
0
 
LVL 1

Expert Comment

by:Jaysin28
ID: 1523127
Copy this code to a new module and you have your routine..


====================================

Option Explicit
Const sErrFileDontExist = "INI File Not Found"
Const sErrSectionDontExist = "INI Section Not Found"
Public sPath As String

Public Function INIFileExists(sFileName As String) As Boolean

Dim bResult As Boolean
Dim sDestFile As String
Dim sDestFileExt() As String
Dim iFileBegin As Integer
Dim bFnrtn As Boolean
Dim i As Integer
   
   bFnrtn = True              ' Assume File Exists
   ' Check to see if sFileName is a valid INI file and it exists.
   If Dir(sFileName) = "" Then
      bFnrtn = False
      Err.Description = sErrFileDontExist
      GoTo MyExit
   End If


MyExit:
   INIFileExists = bFnrtn
   Exit Function

ErrorHandler:
   bFnrtn = False
   Resume MyExit

End Function


Public Function CreateTempINIFileName(sFileName As String) As String
'  This sub creates a temporary file name associated with sFileName
'  the filename that is created is the original filename with an
'  extension that is relative to the number of the files with the same
'  prefix.
'
'
' Returns:
'  String - FileName that was created
'           (if unsuccessful then the loop will keep going)
'           (until it finds a fileName.)
'
' History:
'  02-19-99 JJJ Created.


On Error GoTo ErrorHandler

Dim i As Integer
Dim bResult As Boolean
Dim sDestFile As String
Dim sDestFileExt() As String
Dim iFileBegin As Integer

   ' Initialize the integer couinter that determines the filename extension
   i = 1
   iFileBegin = InStrRev(sFileName, "\")
   If iFileBegin = 0 Then
      ' We didn't find a '\', return empty string.
      sPath = ""
   Else
      sPath = Left(sFileName, iFileBegin)
   End If
   ' Assign the Destination file a name.
   sDestFile = Mid(sFileName, iFileBegin + 1, Len(sFileName))
   sDestFileExt = Split(sDestFile, ".")
   sDestFile = sDestFileExt(0) & ".in" & Trim(Str(i))
   
   ' If the assigned name exists then create a different name.
   Do Until bResult = True
      sDestFile = sPath & sDestFile
      sDestFile = Trim(sDestFile)
      If Dir(sDestFile) = "" Then
         Open sDestFile For Output Access Write As #2
         bResult = True
      Else
         i = i + 1
         sDestFile = sDestFileExt(0) & ".in" & Trim(Str(i))
         bResult = False
      End If
   Loop



MyExit:

   CreateTempINIFileName = sDestFile
   Exit Function

ErrorHandler:

   CreateTempINIFileName = sDestFile
   Resume MyExit

End Function

Public Function RemoveINISection(sSection As String, sFileName As String) As Boolean

On Error GoTo ErrorHandler


Dim bFnrtn As Boolean
Dim bNextSection As Boolean
Dim sDestFile As String
Dim NextLine As String
Dim iSectionLoc As Long

   bNextSection = True
   If INIFileExists(sFileName) Then
      ' Open file for READING only
      Open sFileName For Input Access Read As #1
   Else
      Err.Description = sErrFileDontExist
      GoTo MyExit
   End If
   
   sDestFile = CreateTempINIFileName(sFileName)
   
   Do Until EOF(1)
      Input #1, NextLine
      iSectionLoc = InStr(1, NextLine, "[" & sSection & "]", vbTextCompare)
      If iSectionLoc > 0 Then
         bNextSection = False
         Do While bNextSection = False
            If EOF(1) = False Then
               Input #1, NextLine
               iSectionLoc = InStr(1, NextLine, "[", vbTextCompare)
               If iSectionLoc > 0 Then
                  iSectionLoc = InStr(1, NextLine, "]", vbTextCompare)
                  If iSectionLoc > 0 Then
                     bNextSection = True
                  End If
               End If
            Else
               bNextSection = True
            End If
         Loop
      End If
      Print #2, NextLine
   Loop
   
MyExit:
   RemoveINISection = bFnrtn
   Close
   Kill (sFileName)
   Name sDestFile As sFileName
   Exit Function

ErrorHandler:
   
   bFnrtn = False
   Resume MyExit

End Function


0
 
LVL 2

Expert Comment

by:Torus
ID: 1523128
type mistake. I meant it will delete all the keys in the section
0
 
LVL 1

Expert Comment

by:Jaysin28
ID: 1523129
The function will delete all the keys in that section.
Do you mean you want to delete all the keys and leave the section declaration there.

i.e.

Before:

[Desktop]
key1= somekey
key2= somekey

After:

[Desktop]
key1=
key2=
0
 

Expert Comment

by:pakos
ID: 1523130
ooops, i read ur wuestion, did the project, when i tried to put my answer, smart torus was faster.... :)
i'll put my answer later if torus fails.
0
 
LVL 4

Author Comment

by:wileecoy
ID: 1523131
Although the proposed answer didn't completely answer the question, I did find it useful.

Therefore, rather than reject the question, I decided to award partial points.

I will post another question and award full points to Jaysin28, as his code met all the criteria of the question.

0
 
LVL 2

Expert Comment

by:Torus
ID: 1523132
actually, the function will do

Before:

[Desktop]
key1= somekey
key2= somekey

After:
[Desktop]


If wileecoy want to delete the section name also, the
function cannot do that because it will just leave the
section name there.

If so, jaysin28's answer can achieve what exactly you want
:>
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question