• Moin,

    habe einen Datei-Converter geschrieben, der Registrydateien in AutoIt Code konvertiert.
    Für meine Zwecke reicht es vollkommen aus, vielleicht kann ja noch jemand etwas damit anfangen ...

    Update: Region Tempfile erneuert, Script ist jetzt 3x so schnell ! :thumbup:
    (Das ich da nicht gleich drauf gekommen bin ..., tz tz, manchmal sieht man den Wald vor lauter Bäumen nicht.)

    RegToAu3($sPathRegFile, $flag = 0)

    Spoiler anzeigen
    [autoit]

    ; #FUNCTION# ;===============================================================================
    ;
    ; Name...........: _RegToAu3
    ; Description ...: Converts Registry files to AutoIt code.
    ; Syntax.........: _RegToAu3($sPathRegFile, $flag = 0)
    ; Parameters ....: $sPathRegFile - Path to *.reg file
    ; $flag -
    ; |0 - No changes. (Default)
    ; |1 - Sets the pathes in Value strings to AutoIt Macros.
    ; |2 - Sets HKU to HKCU.
    ; Return values .: Success - An Array, containing lines of AutoIt code.
    ; Failure - Returns 0 and Sets @Error:
    ; |0 - No error.
    ; |1 - Invalid path.
    ; |2 - Invalid *.reg file.
    ; Author ........: Greenhorn
    ; Modified.......:
    ; Remarks .......: Supports REGEDIT4 and Windows Registry Editor Version 5.00.
    ; Readable Valuelength is limited to 32767 Chars!
    ; Flags can be combined.
    ; Related .......:
    ; Link ..........;
    ; Example .......; Yes
    ;
    ; ;==========================================================================================
    Func _RegToAu3($sPathRegFile, $flag = 0)

    [/autoit] [autoit][/autoit] [autoit]

    #Region Tempfile
    Local $fhRegFile, $sRegFile, $iRegAdd, $iVersionRegedit
    Local $sHive, $sHiveKey, $sValueName, $sValueType, $sValue, $sAu3File
    If FileExists($sPathRegFile) Then
    $fhRegFile = FileOpen($sPathRegFile, 0)
    If FileReadLine($fhRegFile, 1) = 'REGEDIT4' Then
    $iVersionRegedit = 4
    ElseIf FileReadLine($fhRegFile, 1) = 'Windows Registry Editor Version 5.00' Then
    $iVersionRegedit = 5
    Else
    Return SetError(2) ; Maybe this isn't a regular *.reg File.
    EndIf
    Else
    Return SetError(1) ; Maybe path is wrong.
    EndIf
    ; Delete line breaks.
    $sRegFile = StringRegExpReplace(FileRead($fhRegFile), '(\\\r\n )', '')
    FileClose(FileWrite(FileOpen(StringReplace($sPathRegFile, '.reg', '.tmp'), 2), $sRegFile))
    FileClose($fhRegFile)
    #EndRegion

    [/autoit] [autoit][/autoit] [autoit]

    Local $arSectionNames = IniReadSectionNames(StringReplace($sPathRegFile, '.reg', '.tmp'))

    [/autoit] [autoit][/autoit] [autoit]

    For $i = 1 To $arSectionNames[0]
    If StringRegExp($arSectionNames[$i], '\A\s?;') Then
    $sAu3File &= $arSectionNames[$i] & @CRLF
    ContinueLoop
    ElseIf StringRegExp($arSectionNames[$i], '\A(HKEY)') Then
    $sHiveKey = "'" & $arSectionNames[$i]
    $iRegAdd = 1
    Else
    $sHiveKey = "'" & StringReplace($arSectionNames[$i], '-HKEY', 'HKEY')
    EndIf
    #Region HiveKey
    $sHive = StringRegExp($sHiveKey, '(HKEY\w*\\)', 1)
    If @extended Then
    Switch $sHive[0]
    Case 'HKEY_CLASSES_ROOT\'
    $sHiveKey = StringReplace($sHiveKey, $sHive[0], 'HKCR\')
    Case 'HKEY_CURRENT_USER\'
    $sHiveKey = StringReplace($sHiveKey, $sHive[0], 'HKCU\')
    Case 'HKEY_LOCAL_MACHINE\'
    $sHiveKey = StringReplace($sHiveKey, $sHive[0], 'HKLM\')
    Case 'HKEY_USERS\'
    If $flag = 2 Or $flag = 3 Then
    $sHiveKey = _HKUsersToHKCUser($sHiveKey)
    Else
    $sHiveKey = StringReplace($sHiveKey, $sHive[0], 'HKU\')
    EndIf
    Case 'HKEY_CURRENT_CONFIG\'
    $sHiveKey = StringReplace($sHiveKey, $sHive[0], 'HKCC\')
    EndSwitch
    EndIf
    $sHiveKey &= "'"
    #EndRegion
    Local $arSection = IniReadSection(StringReplace($sPathRegFile, '.reg', '.tmp'), $arSectionNames[$i])
    If @error Then
    If $iRegAdd Then
    $sAu3File &= 'RegWrite(' & $sHiveKey & ') ; Please check if maybe the valuelimit is exceeded!' & @CRLF
    ContinueLoop
    Else
    $sAu3File &= 'RegDelete(' & $sHiveKey & ') ; Please check if maybe the valuelimit is exceeded!' & @CRLF
    ContinueLoop
    EndIf
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    #Region Valuename
    For $ii = 1 To $arSection[0][0]
    If $arSection[$ii][0] = '@' Then
    $sValueName = "''" ; Default ValueName
    ElseIf StringInStr($arSection[$ii][0], '\\') Then
    $sValueName = StringReplace(StringReplace($arSection[$ii][1], '\\', '\'), '"', "'")
    Else
    $sValueName = StringReplace($arSection[$ii][0], '"', "'") ; ValueName
    EndIf
    If Not $iRegAdd Then ; RegDelete.
    $sAu3File &= 'RegDelete(' & $sHiveKey & $sValueName & ')' & @CRLF
    ContinueLoop
    EndIf
    #EndRegion

    #Region Value
    $sValue = "'"
    Select
    Case StringRegExp($arSection[$ii][1], '(hex\(0?1?4?5?6?8?9?a?\):)') ; Not supported Registry Valuetypes!
    $sAu3File &= '; Can not write ' & $sHiveKey & $sValueName & ': Valuetype is not supported by AutoIt!' & @CRLF
    ContinueLoop
    Case StringInStr($arSection[$ii][1], '\"') ; REG_SZ
    $sValueType = "'REG_SZ'"
    $sValue &= StringTrimLeft(StringTrimRight(StringReplace(StringReplace($arSection[$ii][1], '\\', '\'), '\"', '"'), 1), 1)
    Case StringInStr($arSection[$ii][1], '\\') ; REG_SZ
    $sValueType = "'REG_SZ'"
    $sValue &= StringReplace(StringReplace($arSection[$ii][1], '\\', '\'), '"', '')
    Case StringInStr($arSection[$ii][1], 'dword:') ; REG_DWORD
    $sValueType = "'REG_DWORD'"
    $sValue &= StringReplace($arSection[$ii][1], 'dword:', '')
    Case StringRegExp($arSection[$ii][1], '(hex\(?0?3?\)?:)') ; REG_BINARY
    $sValueType = "'REG_BINARY'"
    $sValue &= StringRegExpReplace(StringReplace($arSection[$ii][1], ',', ''), '(hex\(?0?3?\)?:)', '')
    Case StringRegExp($arSection[$ii][1], '(hex\(0?7\):)') ; REG_MULTI_SZ
    $sValueType = "'REG_MULTI_SZ'"
    If $iVersionRegedit = 4 Then
    $sValue &= _HexToCharString($arSection[$ii][1])
    Else
    $sValue &= _HexToCharString($arSection[$ii][1], 1)
    EndIf
    Case StringRegExp($arSection[$ii][1], '(hex\(0?2\):)') ; REG_EXPAND_SZ
    $sValueType = "'REG_EXPAND_SZ'"
    $sValue &= _HexToCharString($arSection[$ii][1])
    Case Else ; REG_SZ
    $sValueType = "'REG_SZ'"
    $sValue &= StringReplace($arSection[$ii][1], '"', '')
    EndSelect
    If $flag = 1 Or $flag = 3 And $sValueType = "'REG_SZ'" Then
    ;_SetCustomMacroToString($sValue, $arSection[0][0], $arSection[0][0])
    $sValue = _SetPathMacroToString($sValue)
    EndIf
    $sValue &= "'"
    #EndRegion
    If $iRegAdd Then
    $sAu3File &= 'RegWrite(' & $sHiveKey & ', ' & $sValueName & ', ' & $sValueType & ', ' & $sValue & ')' & @CRLF
    Else
    $sAu3File &= 'RegDelete(' & $sHiveKey & ', ' & $sValueName & ')' & @CRLF
    EndIf
    Next
    Next
    $arAu3File = StringSplit(StringStripCR($sAu3File), @LF)
    FileDelete(StringReplace($sPathRegFile, '.reg', '.tmp'))
    Return $arAu3File

    [/autoit] [autoit][/autoit] [autoit]

    EndFunc ;==>_RegToAu3
    ; -------------------------------------------------------------------------------------------
    ; _HexToCharString($sHexValues, $iFlag = 0)
    ; -------------------------------------------------------------------------------------------
    Func _HexToCharString($sHexValues, $iFlag = 0)

    [/autoit] [autoit][/autoit] [autoit]

    Local $sHexANSI, $arHexANSI, $sChar
    If $iFlag Then ; Windows Registry Editor Version 5.00 REG_MULTI_SZ !
    $sHexANSI = StringRegExpReplace(StringReplace(StringReplace(StringReplace($sHexValues, ',00,00,00,00,00', ''), ',00,00,00', ',0a'), ',00', ''), '(hex\(0?\d\):)', '')
    Else ; REGEDIT4 REG_MULTI_SZ & REG_EXPAND_SZ / Windows Registry Editor Version 5.00 REG_EXPAND_SZ !
    $sHexANSI = StringRegExpReplace(StringReplace($sHexValues, ',00', ''), '(hex\(0?\d\):)', '')
    EndIf
    $arHexANSI = StringSplit($sHexANSI, ',')
    For $i = 1 To $arHexANSI[0]
    Switch $arHexANSI[$i]
    Case '0a'
    $sChar &= "' & @LF & '"
    Case '27'
    $sChar &= "' & " & '"' & Chr(39) & '"' & " & '"
    Case Else
    $sChar &= Chr(Dec($arHexANSI[$i]))
    EndSwitch
    Next
    Return $sChar

    [/autoit] [autoit][/autoit] [autoit]

    EndFunc ;==>_HexToCharString

    [/autoit] [autoit][/autoit] [autoit]

    ; -------------------------------------------------------------------------------------------
    ; _HKUsersToHKCUser($sHiveKey)
    ; -------------------------------------------------------------------------------------------
    Func _HKUsersToHKCUser($sHiveKey)

    [/autoit] [autoit][/autoit] [autoit]

    Local $sChangeKey = StringRegExpReplace($sHiveKey, 'HKEY_USERS\\.*?\\', 'HKCU\\')
    Return $sChangeKey

    [/autoit] [autoit][/autoit] [autoit]

    EndFunc ;==>_HKUsersToHKCUser

    [/autoit] [autoit][/autoit] [autoit]

    ; -------------------------------------------------------------------------------------------
    ; _SetPathMacroToString($sString)
    ; -------------------------------------------------------------------------------------------
    Func _SetPathMacroToString($sString)

    Local $arSection = IniReadSection(@ScriptDir & '\pattern.ini', 'DefaultPathMacro')
    Local $sMacroStr, $sAppend = ''
    If @error = 1 Then Return SetError(1)
    ;_ArrayDisplay($arSection)
    For $i = 1 To $arSection[0][0]
    $arResult = StringRegExp($sString, $arSection[$i][1], 3)
    If @error = 0 Then
    If StringInStr($sString, $arResult[0]) Then
    If StringRight($arResult[0], 1) = '\' Then $sAppend = " & '\"
    $sMacroStr = StringTrimLeft(StringReplace($sString, $arResult[0], $arSection[$i][0] & $sAppend), 1)
    Return $sMacroStr
    EndIf
    EndIf
    Next
    Return $sString

    [/autoit] [autoit][/autoit] [autoit]

    EndFunc ;==>_SetPathMacroToString

    [/autoit]

    pattern.ini

    Spoiler anzeigen

    Beispiel:

    [autoit]

    ; #EXAMPLE# _RegToAu3 ;================================================================================
    #include <Array.au3>
    #include 'RegTo.au3'

    [/autoit][autoit][/autoit][autoit]

    RunWait(@WindowsDir & '\regedit.exe /e "' & @ScriptDir & '\AutoIt_v3.reg" "HKEY_CURRENT_USER\Software\AutoIt v3"', @ScriptDir)

    [/autoit][autoit][/autoit][autoit]

    $sPathRegFile = @ScriptDir & '\AutoIt_v3.reg' ; Path to *.reg file

    [/autoit][autoit][/autoit][autoit]

    ; $iFlag:
    ; 1 = Sets the pathes in Value strings to AutoIt Macros.
    ; 2 = Sets HKU to HKCU.
    ; 3 = Sets both.
    For $iFlag = 1 To 3
    $arConvReg = _RegToAu3($sPathRegFile, $iFlag)
    If @error = 1 Then
    MsgBox(0, '', 'Maybe path is wrong.')
    Exit
    ElseIf @error = 2 Then
    MsgBox(0, '', "Maybe this isn't a regular *.reg File.")
    Exit
    EndIf

    [/autoit][autoit][/autoit][autoit]

    _ArrayDisplay($arConvReg)
    Next

    [/autoit][autoit][/autoit][autoit]

    Exit
    ; ;==========================================================================================

    [/autoit]


    Gruß
    Greenhorn

    source


    9 Mal editiert, zuletzt von Greenhorn (13. April 2008 um 06:35)

  • Ich frage mich gerade wie die C:\Re.reg ausgebaut sein soll? Oder wie kommt man an diese Daten ran. Würde es ja gerne mal ausprobieren. Und Jungs? Kommt mir jetzt nicht mit "Mal spielt nicht mit der Registry". LOL

    Mir fällt gerade ein, das kann ja ein exp. Registrykey sein. Aber wenn das der Fall ist, dann die frage, über wieviele Ebene das funktioniert. Gibt ja ab und an, das die Schlüssel wiederum einige Unterschlüssel haben, welche selber bereits der dritte Unterschlüssel ist. Also eine längere Verästelung.

    LG, Lina.

    Lieben Gruß,
    Alina

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Geheime Information: ;)
    OuBVU5ebLhHu5QvlnAyQB4A7SzBrvWulwL7RLl2BdH5tI6sIYspeMKeXMSXl

  • Ja, das ist verständlich.

    LG und Dank.

    Lieben Gruß,
    Alina

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Geheime Information: ;)
    OuBVU5ebLhHu5QvlnAyQB4A7SzBrvWulwL7RLl2BdH5tI6sIYspeMKeXMSXl

  • Beispiel etwas verbessert ...

    [autoit]

    ; #EXAMPLE# _RegToAu3 ;================================================================================
    #include <Array.au3>
    #include 'RegTo.au3'

    [/autoit][autoit][/autoit][autoit]

    RunWait(@WindowsDir & '\regedit.exe /e "' & @ScriptDir & '\AutoIt_v3.reg" "HKEY_CURRENT_USER\Software\AutoIt v3"', @ScriptDir)

    [/autoit][autoit][/autoit][autoit]

    $sPathRegFile = @ScriptDir & '\AutoIt_v3.reg' ; Path to *.reg file

    [/autoit][autoit][/autoit][autoit]

    ; $iFlag:
    ; 1 = Sets the pathes in Value strings to AutoIt Macros.
    ; 2 = Sets HKU to HKCU.
    ; 3 = Sets both.
    For $iFlag = 1 To 3
    $arConvReg = _RegToAu3($sPathRegFile, $iFlag)
    If @error = 1 Then
    MsgBox(0, '', 'Maybe path is wrong.')
    Exit
    ElseIf @error = 2 Then
    MsgBox(0, '', "Maybe this isn't a regular *.reg File.")
    Exit
    EndIf

    [/autoit][autoit][/autoit][autoit]

    _ArrayDisplay($arConvReg)
    Next

    [/autoit][autoit][/autoit][autoit]

    Exit
    ; ;==========================================================================================

    [/autoit]