Jump to content

_RegToScript


Arilvv
 Share

Recommended Posts

If a line in script is too long, autoit3 can't handle with it. So this function will cut it by using a variable $Value .

;===============================================================================
; Description:    Export registry key to an autoit script file
; Usage          _RegToScript($File, $KeyName [,$ValueName])
; Parameter(s):  $File - The file to write to
;                  $KeyName - The registry key to export
;                  $ValueName - The value to export
; Requirement(s):   None
; Return Value(s):  None
; Author(s):        Arilvv
;===============================================================================

Func _RegToScript($File, $KeyName, $ValueName = "")
    Dim $RegTypes[11] = ["REG_NONE", "REG_SZ", "REG_EXPAND_SZ", "REG_BINARY", "REG_DWORD", "REG_DWORD_BIG_ENDIAN", "REG_LINK", "REG_MULTI_SZ", "REG_RESOURCE_LIST", "REG_FULL_RESOURCE_DESCRIPTOR", "REG_RESOURCE_REQUIREMENTS_LIST"]
    Local $i, $Vaule, $SubKeyName, $KeyNameString, $TempStr, $EntireKey = False

    If @NumParams = 2 Then $EntireKey = True

    $KeyNameString = StringReplace($KeyName, '"', '""')
    $i = 1
    While True
        If $EntireKey Then
            $ValueName = RegEnumVal($KeyName, $i)
            If @Error Then ExitLoop
            $Value = RegRead($KeyName, $ValueName)
            If @Error Then ExitLoop
        Else
            $Value = RegRead($KeyName, $ValueName)
            If @Error Then Return
        EndIf

        $TypeName = $RegTypes[@Extended]
        $ValueName = StringReplace($ValueName, '"', '""')

        If StringLen($Value) <= 1024 Then
            $Value = StringReplace($Value, '"', '""')
            If $TypeName = "REG_MULTI_SZ" Then $Value = StringReplace($Value, @LF, '" & @LF & "')
            FileWriteLine($File, 'RegWrite("$KeyNameString$", "$ValueName$", "$TypeName$", "$Value$")')
        Else
            FileWriteLine($File, '$$Value = ""; Long String For $KeyNameString$\$ValueName$')

            While $Value <> ""
                $TempStr = StringReplace(StringLeft($Value, 1024), '"', '""')
                If $TypeName = "REG_MULTI_SZ" Then $TempStr = StringReplace($TempStr, @LF, '" & @LF & "')
                FileWriteLine($File, '$Value &= "' & $TempStr & '"')
                $Value = StringTrimLeft($Value, 1024)
            WEnd
            FileWriteLine($File, 'RegWrite("$KeyNameString$", "$ValueName$", "$TypeName$", $$Value)')
        EndIf

        If Not $EntireKey Then Return
        $i += 1
    Wend

    $i = 1
    While True
        $SubKeyName = RegEnumKey($KeyName, $i)
        If @Error Then ExitLoop
        _RegToScript($File, $KeyName & "\" & $SubKeyName)
        $i += 1
    WEnd
EndFunc

Example:

$File = FileOpen("Test.au3", 2)
_RegToScript($File, "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer")
FileClose($File)
Edited by Arilvv
Link to comment
Share on other sites

  • 8 months later...

I was having trouble making the > 1024 long string code work, and here is what I wound up with:

FileWriteLine($File, '$Value = ""; Long String For ' & $KeyNameString & '\' & $ValueName)
           
            While $Value <> ""
                $TempStr = StringReplace(StringLeft($Value, 1024), '"', '""')
                If $TypeName = "REG_MULTI_SZ" Then $TempStr = StringReplace($TempStr, @LF, '" & @LF & "')
                FileWriteLine($File, '$Value &= "' & $TempStr & '"')
                $Value = StringTrimLeft($Value, 1024)
            WEnd
           
            FileWriteLine($File, 'RegWrite("'&$KeyNameString&'", "'&$ValueName&'", "'&$TypeName&'", $Value)')

Thanks for the code.

Link to comment
Share on other sites

Here is the above fix plus several enhancements I made. The function now returns the number of registry entries written. I also added (really exposed) a new function parameter. Please see the examples at the top.

;===============================================================================
; Description:    Export registry key to an autoit script file
; Usage          _RegToScript($File, $KeyName [,$ValueName],[$EntireKey])
; Parameter(s):  $File - The file to write to
;                  $KeyName - The registry key to export
;                  $ValueName - The value to export
;                  $EntireKey - True exports entire key, $ValueName is ignored
; Requirement(s):   None
; Return Value(s):  Number of registry entries written
; Author(s):        Arilvv
; Modifications:    Dave Pearce (Oct 2006)
; Examples:
;       _RegToScript($File, "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft")          ; Exports all values for this key and it's subkeys
;       _RegToScript($File, "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft", "")      ; Exports the default value for this key
;       _RegToScript($File, "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft", "Home")  ; Exports the value "Home" for this key
;       _RegToScript($File, "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft", "", True); Exports all values for this key and it's subkeys
;===============================================================================


Func _RegToScript($File, $KeyName, $ValueName = "", $EntireKey = False)
    Dim $RegTypes[11] = ["REG_NONE", "REG_SZ", "REG_EXPAND_SZ", "REG_BINARY", "REG_DWORD", "REG_DWORD_BIG_ENDIAN", "REG_LINK", "REG_MULTI_SZ", "REG_RESOURCE_LIST", "REG_FULL_RESOURCE_DESCRIPTOR", "REG_RESOURCE_REQUIREMENTS_LIST"]
    Local $i, $Value, $SubKeyName, $KeyNameString, $TempStr, $rc = 0

    If @NumParams = 2 Then $EntireKey = True

    $KeyNameString = StringReplace($KeyName, '"', '""')
    $i = 1
    While True
        If $EntireKey Then
            $ValueName = RegEnumVal($KeyName, $i)
            If @Error Then ExitLoop
            $Value = RegRead($KeyName, $ValueName)
            If @Error Then ExitLoop
        Else
            $Value = RegRead($KeyName, $ValueName)
            If @Error Then Return $rc; value supplied but key not found
        EndIf

        $TypeName = $RegTypes[@Extended]
        $ValueName = StringReplace($ValueName, '"', '""')

        If StringLen($Value) <= 1024 Then
            $Value = StringReplace($Value, '"', '""')
            If $TypeName = "REG_MULTI_SZ" Then $Value = StringReplace($Value, @LF, '" & @LF & "')
            FileWriteLine($File, 'RegWrite("'&$KeyNameString&'", "'&$ValueName&'", "'&$TypeName&'", "'&$Value&'")')
            $rc += 1
        Else
           FileWriteLine($File, '$Value = ""; Long String For ' & $KeyNameString & '\' & $ValueName)
            $rc += 1

           
            While $Value <> ""
                $TempStr = StringReplace(StringLeft($Value, 1024), '"', '""')
                If $TypeName = "REG_MULTI_SZ" Then $TempStr = StringReplace($TempStr, @LF, '" & @LF & "')
                FileWriteLine($File, '$Value &= "' & $TempStr & '"')
                $rc += 1
                $Value = StringTrimLeft($Value, 1024)
            WEnd
           ; FileWriteLine($File, 'RegWrite("' & $KeyNameString & '", "' & $ValueName & '", "' & $TypeName & '", "' & $Value) & ")')
            FileWriteLine($File, 'RegWrite("'&$KeyNameString&'", "'&$ValueName&'", "'&$TypeName&'", $Value)')
            $rc += 1


        EndIf

        If Not $EntireKey Then Return $rc; Value found
        $i += 1
    Wend

    $i = 1
    While True
        $SubKeyName = RegEnumKey($KeyName, $i)
        If @Error Then ExitLoop
        $rc += _RegToScript($File, $KeyName & "\" & $SubKeyName)
        $i += 1
    WEnd
    
    Return $rc; Key not found & found
EndFunc


$Key = "HKEY_LOCAL_MACHINE\SOFTWARE\Mozilla"
$Value = "Ignored"
$Everything = True

$File = FileOpen("Test.au3", 2)
$r = _RegToScript($File, $Key, $Value, $Everything)
FileClose($File)

Exit $r
Link to comment
Share on other sites

  • 4 months later...

Again, this script is awesome and has saved me a huge amount of work. I thought you might want to know that I did encounter one instance when it didn't work correctly. I have a REG_SZ value that has a carriage return in the middle of it and when I extract it to an .au3 file it carries the carriage return with it which splits the line of code and causes the script to fail. For example, if I export that value using regedit4 it comes out as:

"szLeftTabs"="C:\\

D:\\"

Which translates as:

RegWrite("HKEY_CURRENT_USER\Software\ZabaraKatranemia Plc\xplorer2\MainFrame Settings", "szLeftTabs", "REG_SZ", "C:\

D:\")

Any idea how to fix this? If not it's still a fantasticly useful script since this is a rare error.

Thanks

Link to comment
Share on other sites

  • 7 months later...

I know this is an older thread but on a related note...

Has anyone figured out how to use AutoIt to export a registry key as .reg file? That means using it to perform regedit's Export Key function.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

I know this is an older thread but on a related note...

Has anyone figured out how to use AutoIt to export a registry key as .reg file? That means using it to perform regedit's Export Key function.

Will the command line switches for regedit work for you?

RunWait('regedit /E hklm_run.reg "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run"')
Link to comment
Share on other sites

Will the command line switches for regedit work for you?

RunWait('regedit /E hklm_run.reg "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run"')
Well that shows how sharp I am. I even have those command line switches posted on my website. <_<

Thanks ResNullius

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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