Bei INI-Dateien mit mehr als 5 Keys pro Section fand ich es etwas umständlich, die Keys zu suchen, welche keinen Wert mehr haben. Also überlegte ich mir einen einfacheren Weg. Gut, es hat eine ganze Weile gedauert, bis es wie gewünscht funktionierte. Nun möchte ich die Funktion mit Euch teilen.
Mit _ini_delete_keys werden alle Keys einer Section gelöscht. Eine Fehler- bzw. Erfolgsausgabe gibt es nicht.
Code: _ini_delete_keys
#include <Array.au3>
#include <File.au3>
; #FUNCTION# ====================================================================================================================
; Name ..........: _ini_delete_keys
; Description ...: Delete all keys of one INI-section
; Syntax ........: _ini_delete_keys($iniSection, $sFilePath)
; Parameters ....: $iniSection - The section in which all keys should be deleted.
; $sFilePath - The path to the INI-file
; Return values .: None
; Author ........: MacG (https://autoit.de)
; ===============================================================================================================================
Func _ini_delete_keys($iniSection, $sFilePath)
Local $aFileData
_FileReadToArray($sFilePath, $aFileData)
For $i = 1 To $aFileData[0]
If StringInStr($aFileData[$i], $iniSection) Then
$iDelBegin = $i + 1
For $j = $iDelBegin To $aFileData[0]
If StringInStr($aFileData[$j], '[') Then
$iDelEnd = $j - 1
ExitLoop
EndIf
Next
ExitLoop
EndIf
Next
For $i = $iDelBegin To $iDelEnd
_ArrayDelete($aFileData, $iDelBegin)
Next
_ArrayInsert($aFileData, $iDelBegin, '') ;add a blank line to the section
_FileWriteFromArray($sFilePath, $aFileData, 1)
EndFunc ;==>_ini_delete_keys
Alles anzeigen
Direkt danach schreibe ich in meinem Programm die Keys dieser Section erneut in die INI-Datei. Damit sind die Keys auch immer gut sortiert, auch wenn mal später noch ein Key hinzugefügt wird.
EDIT:
Wie es weiter unten steht, nutzt: IniWriteSection($sFilePath, $iniSection, '') es bewirkt genau das Gleiche.