Jump to content
Sign in to follow this  
Greenhorn

Yet another RegTo.au3

Recommended Posts

Greenhorn

Moin,

before you beat me: I know there are some scripts here available with the same, or nearly the same, result.

But my working engine is another than the others ...

A handycap of my script is, that the readable valuelength is limited to 32767 characters, cause I use IniRead functions.

But for my usage it is enough. :)

I also added a function to change directory strings in values to AutoIt Macros.

This function works with english and german directory strings.

O.K., here it is ...

Example

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

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

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

; $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

    _ArrayDisplay($arConvReg)
Next

ExitoÝ÷ Ù±nËb¢Ûjëh×6; ;===============================================================================
;
; 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)

    #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

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

    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

        #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
                $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

EndFunc   ;==>_RegToAu3

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

    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

EndFunc   ;==>_HexToCharString

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

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

EndFunc   ;==>_HKUsersToHKCUser

; -------------------------------------------------------------------------------------------
; _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

EndFunc   ;==>_SetPathMacroToString

pattern.ini

[DefaultPathMacro]
      @CommonFilesDir    = ([a-zA-Z]:\\Programs?m?e?\\(Gemeinsame Dateien|Common Files)\\?)
      @ProgramFilesDir  = ([a-zA-Z]:\\Programs?m?e?\\)
      @AppDataCommonDir   = ([a-zA-Z]:\\(Dok?c?umente?s? a?u?nd S?E?\w*?\\All Users\\)(Anwendungsdaten|Application Data)\\?)
      @AppDataDir        = ([a-zA-Z]:\\Dok?c?umente?s? a?u?nd S?E?\w*?\\.*?\\(Anwendungsdaten|Application Data)\\?)
      @ProgramsCommonDir  = ([a-zA-Z]:\\(Dok?c?umente?s? a?u?nd S?E?\w*?\\All Users\\)(Startmenü|Start Menu)(\\Programs?m?e?)\\?)
      @ProgramsDir      = ([a-zA-Z]:\\(Dok?c?umente?s? a?u?nd S?E?\w*?\\.*?\\)(Startmenü|Start Menu)(\\Programs?m?e?)?\\?)
      @StartMenuCommonDir = ([a-zA-Z]:\\(Dok?c?umente?s? a?u?nd S?E?\w*?\\All Users\\)(Startmenü|Start Menu)\\?)
      @StartMenuDir    = ([a-zA-Z]:\\(Dok?c?umente?s? a?u?nd S?E?\w*?\\.*?\\)(Startmenü|Start Menu)\\?)
      @DocumentsCommonDir = ([a-zA-Z]:\\(Dok?c?umente?s? a?u?nd S?E?\w*?\\All Users\\Doc?k?uments?e?)\\?)
      @DesktopCommonDir   = ([a-zA-Z]:\\(Dok?c?umente?s? a?u?nd S?E?\w*?\\All Users\\Desktop)\\?)
      @DesktopDir        = ([a-zA-Z]:\\Dok?c?umente?s? a?u?nd S?E?\w*?\\.*?\\Desktop\\?)
      @MyDocumentsDir    = ([a-zA-Z]:\\(.*?\\)?(.*?\\)?(Eigene Dateien|My Documents)\\?)
      @SystemDir          = ([a-zA-Z]:\\WINDOWS\\system\\?)
      @WindowsDir        = ([a-zA-Z]:\\WINDOWS\\?)

That's all, folks.

Greetz

Greenhorn

source

updated.zip

Edited by Greenhorn

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.