Jump to content

.ini compare / merge / synchronize


 Share

Recommended Posts

This script (still in testing form) eats 2 .ini configuration files and synchronizes their contents into a new .ini without overwriting any existing values for keys present in both ini's. Later on something like BeyondCompare could be used to check results. :)

#include <array.au3>
Global $arrCommonSecs[1]
Global $arrUniqueSecsLeft[1]
Global $arrUniqueSecsRight[1]
Global $iniOut = "iniOut.ini"
Global $iniOut_old = "iniOut_old.ini"
Global $check = 0
; debug
Global $dumpBoth = "dumpCommonSections.txt"
Global $dumpUniqueLeft = "dumpUniqueSectionsLeft.txt"
Global $dumpUniqueRight = "dumpUniqueSectionsRight.txt"
; end debug

$iniLeft = FileOpenDialog("Select ini 1", @ScriptDir, "All (*.*)", 1)
If @error Then Exit
$iniRight = FileOpenDialog("Select ini 2", @ScriptDir, "All (*.*)", 1)
If @error Then Exit

$arrSecNamesLeft = IniReadSectionNames($iniLeft)
$arrSecNamesRight = IniReadSectionNames($iniRight)
For $i = 1 To $arrSecNamesLeft[0]
    For $j = 1 To $arrSecNamesRight[0]
        If $arrSecNamesRight[$j] = $arrSecNamesLeft[$i] Then
            _ArrayAdd($arrCommonSecs, $arrSecNamesRight[$j])
            ExitLoop
        EndIf
    Next
Next
$arrCommonSecs[0] = UBound($arrCommonSecs) - 1
If FileExists(@ScriptDir & "\" & $iniOut) Then ; backup old iniout
    $filecopy = FileMove(@ScriptDir & "\" & $iniOut, @ScriptDir & "\" & $iniOut_old, 1)
    If $filecopy = 0 Then
        MsgBox(64, "Error 1.a", "Failed to backup old ini out")
        Exit 1
    EndIf
EndIf
$filecopy = FileCopy($iniLeft, @ScriptDir & "\" & $iniOut, 0) ; use a copy of left ini as new iniout
If $filecopy = 0 Then
    MsgBox(64, "Error 1.b", "Failed to copy first ini")
    Exit 1
EndIf
WriteCommonSecs()
ArrayToFileDumper($dumpBoth, $arrCommonSecs) ; debug

For $i = 1 To $arrSecNamesLeft[0]
    $check = 0
    For $j = 1 To $arrCommonSecs[0]
        If $arrCommonSecs[$j] = $arrSecNamesLeft[$i] Then
            $check = 1
            ExitLoop
        EndIf
    Next
    If $check = 0 Then _ArrayAdd($arrUniqueSecsLeft, $arrSecNamesLeft[$i])
Next
$arrUniqueSecsLeft[0] = UBound($arrUniqueSecsLeft) - 1
For $i = 1 To $arrUniqueSecsLeft[0] ; write unique sections to ini
    $arrSection = IniReadSection($iniLeft, $arrUniqueSecsLeft[$i])
    IniWriteSection($iniOut, $arrUniqueSecsLeft[$i], $arrSection)
Next
ArrayToFileDumper($dumpUniqueLeft, $arrUniqueSecsLeft) ; debug

For $i = 1 To $arrSecNamesRight[0]
    $check = 0
    For $j = 1 To $arrCommonSecs[0]
        If $arrCommonSecs[$j] = $arrSecNamesRight[$i] Then
            $check = 1
            ExitLoop
        EndIf
    Next
    If $check = 0 Then _ArrayAdd($arrUniqueSecsRight, $arrSecNamesRight[$i])
Next
$arrUniqueSecsRight[0] = UBound($arrUniqueSecsRight) - 1
For $i = 1 To $arrUniqueSecsRight[0] ; write unique sections to ini
    $arrSection = IniReadSection($iniRight, $arrUniqueSecsRight[$i])
    IniWriteSection($iniOut, $arrUniqueSecsRight[$i], $arrSection)
Next
ArrayToFileDumper($dumpUniqueRight, $arrUniqueSecsRight) ; debug

Func WriteCommonSecs()
    Local $test = 0
    Local $arrSecLeft[1]
    Local $arrSecRight[1]
    For $i = 1 To $arrCommonSecs[0] ; merge keys for dupe entries from 2nd ini to a copy of the first one
        $arrSecLeft = IniReadSection($iniLeft, $arrCommonSecs[$i])
        $arrSecRight = IniReadSection($iniRight, $arrCommonSecs[$i])
        If IsArray($arrSecRight) And IsArray($arrSecLeft) Then
            For $j = 1 To $arrSecRight[0][0]
                For $k = 1 To $arrSecLeft[0][0]
                    ;ToolTip("$j " & $j & " of " & $arrSecRight[0][0] & "| $k " & $k & " of " & $arrSecLeft[0][0], 300, 0)
                    If $arrSecRight[$j][0] = $arrSecLeft[$k][0] Then ; keys match within section, check if left is blank, otherwise leave as is
                        If Not $arrSecRight[$j][1] = $arrSecLeft[$k][1] Then ; if values don't match
                            If StringLen($arrSecLeft[$k][1]) = 0 And StringLen($arrSecRight[$j][1]) > 0 Then
                                $write = IniWrite($iniOut, $arrCommonSecs[$i], $arrSecRight[$j][0], $arrSecRight[$j][1])
                                If $write = 0 Then
                                    MsgBox(64, "Error 2", "Couldn't write new value for existing key to existing section")
                                    Exit 1
                                EndIf
                            EndIf
                        EndIf
                        $test = 1
                        ExitLoop
                    Else ; keys don't match
                        $test = 0
                    EndIf
                Next
                If $test = 0 Then ; new key
                    ;ToolTip("$j " & $j & " of " & $arrSecLeft[0][0] & "| $k " & $test & " of " & $arrSecRight[0][0],300,0)
                    $write = IniWrite($iniOut, $arrCommonSecs[$i], $arrSecRight[$j][0], $arrSecRight[$j][1])
                    If $write = 0 Then
                        MsgBox(64, "Error 3", "Couldn't write new key to existing section")
                        Exit 1
                    EndIf
                EndIf
            Next
        ElseIf IsArray($arrSecRight) Then ; first ini has no keys under section name
            For $l = 1 To $arrSecRight[0][0]
                $write = IniWrite($iniOut, $arrCommonSecs[$i], $arrSecRight[$l][0], $arrSecRight[$l][1])
                If $write = 0 Then
                    MsgBox(64, "Error 4", "Couldn't batch write new keys to existing empty section")
                    Exit 1
                EndIf
            Next
        EndIf
    Next
EndFunc   ;==>WriteCommonSecs

Func ArrayToFileDumper($zfile, ByRef $zdump)
    $hwnd = FileOpen(@ScriptDir & "\" & $zfile, 2) ; array dump
    For $i = 1 To $zdump[0]
        FileWriteLine($hwnd, $zdump[$i])
    Next
    FileClose($hwnd)
EndFunc   ;==>ArrayToFileDumper
Edited by SmartiePants

[font="Comic Sans MS"]It's my first day.[/font]View and move images to subdirectories by pressing a key in XnView

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...