Jump to content
Sign in to follow this  
SlimShady

Extended INIDelete

Recommended Posts

SlimShady

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

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.