Change Mp3tag Tools.ini for Mp3tagCompleteTags.exe (Version 2)
;====================================================================================================================================================
; (c)Detlev Dalitz.20120116.
;
; Change or add an entry in Mp3tag's "Tools.ini" for Dano's tool "Mp3tagCompleteTags.exe".
;
; Using Regular Expressions.
;====================================================================================================================================================

intPrevIC50 = IntControl (50, 0, 0, 0, 0) ; Remove "Go to web page" button from error boxes.
DirChange (DirScript ())

; Define files names.
strFileExe = Environment ("ProgramFiles") : "\Mp3tag\Mp3tagCompleteTags.exe" ; Adapt this line to the actual install path of "Mp3tagCompleteTags.exe".
strFileToolsIni = Environment ("APPDATA") : "\Mp3tag\data\tools.ini"   ; Filepath to the Mp3tag "Tools.ini".
strFileToolsIniTmp = ItemReplace ("tmp.ini", -1, strFileToolsIni, ".") ; Use filename "Tools.tmp.ini".

; Define ini file entries as it should be.
strToolKeyName = "MTTOOLSNAME"
strToolKeyParam = "MTTOOLSPARAM"
strToolKeyPath = "MTTOOLSPATH"
strToolKeyInst = "MTTOOLSINST"
strToolValueName = "Mp3tagCompleteTags"
strToolValueParam = `'"'%%_workingpath%%'"'`
strToolValuePath = StrReplace (strFileExe, "\", "\\")
strToolValueInst = "0"

; Define Regular Expressions.
strRESection = "(\[#\d+\][\r\n]+" : strToolKeyName : "=" : strToolValueName : "[\r\n]+.+?[\r\n]+.+?[\r\n]+.+?[\r\n]+)"
strRESectionName = "\[(.+?)\]"
strRESectionNr = "\[#(\d+)\]"
strREToolName = strToolKeyName : "=(.+?)[\r\n]+"
strREToolParam = strToolKeyParam : "=(.+?)[\r\n]+"
strREToolPath = strToolKeyPath : "=(.+?)[\r\n]+"
strREToolInst = strToolKeyInst : "=(.+?)[\r\n]+"

;----------------
; Example ini section.
;
;   [#32]
;   MTTOOLSNAME=Mp3tagCompleteTags
;   MTTOOLSPARAM='"'%_workingpath%'"'
;   MTTOOLSPATH=X:\\Folder1\\Folder2\\Mp3tagCompleteTags\\Mp3tagCompleteTags.exe
;   MTTOOLSINST=0
;----------------

; Get the complete ini file into string variable.
strIniText = FileGet (strFileToolsIni)

; Create Regular Expression object interface and define settings.
; Entire Section.
objRegExp1 = ObjectCreate ("VBScript.RegExp")
objRegExp1.IgnoreCase = @TRUE
objRegExp1.Global = @TRUE
objRegExp1.MultiLine = @TRUE

; Get existing section, if any.
objRegExp1.Pattern = strRESection
If objRegExp1.Test(strIniText)
   ; Existing section found.
   ; Change section entries.

   strText = objRegExp1.Execute(strIniText).item(0).SubMatches.item(0)
   ;   objMatch = objRegExp1.Execute(strIniText).item(0)
   ;   strText = objMatch.SubMatches.item(0)
   ;
   ;   strTest = objMatch.SubMatches.item(0)
   ;   intPos = objMatch.FirstIndex
   ;   Pause ("Result at: " : intPos, strText)

   ; Create Regular Expression object interface and define settings.
   objRegExp2 = ObjectCreate ("VBScript.RegExp")
   objRegExp2.IgnoreCase = @TRUE
   objRegExp2.Global = @FALSE
   objRegExp2.MultiLine = @FALSE

   ; MTTOOLSNAME.
   ; No change.

   ; MTTOOLSPARAM.
   objRegExp2.Pattern = strREToolParam
   If objRegExp2.Test(strText)
      ;   objMatch = objRegExp2.Execute(strText).item(0)
      ;   strTest = objMatch.SubMatches.item(0)
      ;   intPos = objMatch.FirstIndex
      ;   Pause ("Result at: " : intPos, strTest)
      strText = objRegExp2.Replace(strText, : strToolKeyParam : "=" : strToolValueParam : @CRLF)
   EndIf

   ; MTTOOLSPATH.
   objRegExp2.Pattern = strREToolPath
   If objRegExp2.Test(strText)
      ;   objMatch = objRegExp2.Execute(strText).item(0)
      ;   strTest = objMatch.SubMatches.item(0)
      ;   intPos = objMatch.FirstIndex
      ;   Pause ("Result at: " : intPos, strTest)
      strText = objRegExp2.Replace(strText, : strToolKeyPath : "=" : strToolValuePath : @CRLF)
   EndIf

   ; MTTOOLSINST.
   objRegExp2.Pattern = strREToolInst
   If objRegExp2.Test(strText)
      ;   objMatch = objRegExp2.Execute(strText).item(0)
      ;   strTest = objMatch.SubMatches.item(0)
      ;   intPos = objMatch.FirstIndex
      ;   Pause ("Result at: " : intPos, strTest)
      strText = objRegExp2.Replace(strText, : strToolKeyInst : "=" : strToolValueInst : @CRLF)
   EndIf

   objRegExp2 = 0 ; Close RegExp object.

   strIniText = objRegExp1.Replace(strIniText, : strText : @CRLF) ; The new content for the ini file.

Else
   ; Append new section to ini file.
   objRegExp1.Pattern = strRESectionNr
   If objRegExp1.Test(strIniText)
      intCount = objRegExp1.Execute(strIniText).Count ; Number of sections.
      ; Note: Mp3tag ini file sections start from zero [#0], so intCount is the next new section number.
      ; Create section string.
      strSection = "[#" : intCount : "]"
      strSection = strSection : @CRLF : strToolKeyName : "=" : strToolValueName
      strSection = strSection : @CRLF : strToolKeyParam : "=" : strToolValueParam
      strSection = strSection : @CRLF : strToolKeyPath : "=" : strToolValuePath
      strSection = strSection : @CRLF : strToolKeyInst : "=" : strToolValueInst
      strIniText = strIniText : @CRLF : strSection : @CRLF
   EndIf
EndIf

objRegExp1 = 0 ; Close RegExp object.

intBytesWritten = FilePut (strFileToolsIniTmp, strIniText)
; Run (strFileToolsIniTmp, "")

; Copy our temp ini file to the original ini file name.
blnResult = FileCopy (strFileToolsIniTmp, strFileToolsIni, @TRUE) ; With confirm dialog.
; blnResult = FileCopy (strFileToolsIniTmp, strFileToolsIni, @FALSE) : Without confirm dialog.
; Run (strFileToolsIni, "")

:CANCEL
; Remove our temp file.
If 1 == FileExist (strFileToolsIniTmp) Then blnResult = FileDelete (strFileToolsIniTmp)
Exit
;====================================================================================================================================================