Jump to content

Extended INIDelete


SlimShady
 Share

Recommended Posts

You can now delete whole INI sections with my INIDelete function.

And when you specify a key to delete, my function executes the built-in function.

Examples at the bottom.

Edit:

- Added some error checking

The function:

Func _INIDelete($gINIFile, $gINISection, $gINIKey = "")
   Local $i
   Local $CurLine
   Local $INISize
   Local $mINIKey
   Local $INIArray
   Local $mINISect
   Local $INIContent
   Local $OpenINI_write
   
   If $gINIKey <> "" Then
      Local $Err
      $Err = INIDelete($gINIFile, $gINISection, $gINIKey)
      Return $Err
   EndIf
   
   $mINIKey = 0
   $mINISect = 0
   
   If NOT FileExists($gINIFile) Then
      SetError(1)
      Return 0
   Else
      $INISize = FileGetSize($gINIFile)
      $INIContent = FileRead($gINIFile, $INISize)
      If NOT StringInStr($INIContent, @LF) Then
         SetError(1)
         Return 0
      Else
         $INIArray = StringSplit(StringStripCR($INIContent), @LF)
      EndIf
      $INIContent = 0
   EndIf
   
   $OpenINI_write = FileOpen($gINIFile, 2)
   
   For $i = 1 To $INIArray[0]
      $CurLine = StringStripWS($INIArray[$i], 7)
      If StringLeft($CurLine, StringLen($gINISection) + 2) = "[" & $gINISection & "]" _
         OR StringLeft($CurLine, StringLen($gINISection) + 4) = "[ " & $gINISection & " ]" Then
            $mINISect = 1
      ElseIf StringLeft($CurLine, 1) = "[" Then
         $mINISect = 0
      EndIf
      If NOT $mINISect Then
         If $CurLine <> "" Then FileWriteLine($OpenINI_write, $INIArray[$i])
      EndIf
   Next
   $INIArray = 0
   FileClose($OpenINI_write)
   Return 1
EndFunc

Example 1:

;Deletes section "Settings" from Test.ini
_INIDelete(@ScriptDir & "\Test.ini", "Settings")

Example 2:

;Deletes section "Settings" from Test.ini
_INIDelete(@ScriptDir & "\Test.ini", "Settings", "")

Example 3:

;Deletes key "Width" from section "Settings" from Test.ini
_INIDelete(@ScriptDir & "\Test.ini", "Settings", "Width")
Edited by SlimShady
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...