Jump to content

_RegCopyKey()


big_daddy
 Share

Recommended Posts

  • Moderators

$sRegHive = "HKEY_LOCAL_MACHINE"
$sRegKey = "SOFTWARE\AutoIt v3\AutoIt"
$sRegKeyNew = "SOFTWARE\AutoIt v3\AutoIt New"

_RegCopyKey($sRegHive, $sRegKey, $sRegKeyNew, False)
If @error Then
    MsgBox(48, "Error", "There was an error while copying the key.")
Else
    MsgBox(64, "Success", "The key was copied successfully.")
EndIf

;===============================================================================
;
; Function Name:    _RegCopyKey
; Description::     Copies a registry key.
; Parameter(s):     $s_RegHive      - The registry hive of the key.
;                   $s_RegKey       - The registry key to copy from.
;                   $s_RegKeyNew    - The registry key to copy to.
;                   $f_Delete       - Specifies whether to delete the original key.
; Requirement(s):   AutoIt v3.2.0.1
; Return Value(s):  On Success  - Returns 1
;                   On Failure  - Returns 0 and sets @ERROR = 1
; Author(s):        Bob Anthony (big_daddy)
;
;===============================================================================
;
Func _RegCopyKey($s_RegHive, $s_RegKey, $s_RegKeyNew, $f_Delete = False)
    Local $s_TempRegFile = @TempDir & "\~regfile.reg"
    
    FileDelete($s_TempRegFile)
    RunWait("regedit /e:a " & $s_TempRegFile & ' "' & $s_RegHive & "\" & $s_RegKey & '"')
    If Not FileExists($s_TempRegFile) Then
        SetError(1)
        Return 0
    EndIf
    
    $s_File = FileRead($s_TempRegFile)
    If @error Then
        SetError(1)
        Return 0
    EndIf
    
    $s_FileNew = StringReplace($s_File, $s_RegKey, $s_RegKeyNew)
    If @error Or @extended = 0 Or $s_FileNew = "" Then
        SetError(1)
        Return 0
    EndIf
    
    $h_File = FileOpen($s_TempRegFile, 2)
    If $h_File = -1 Then
        SetError(1)
        Return 0
    EndIf
    
    $f_Return = FileWrite($h_File, $s_FileNew)
    If Not $f_Return Then
        SetError(1)
        Return 0
    EndIf
    
    FileClose($h_File)
    
    If $f_Delete Then
        RegDelete($s_RegKey)
    EndIf
    
    RunWait("regedit.exe /s " & $s_TempRegFile)
    FileDelete($s_TempRegFile)
    SetError(0)
    Return 1
EndFunc   ;==>_RegCopyKey

Link to comment
Share on other sites

First of all, I want to thank you for adding those comments, it has helped my a great deal in understanding your code. :whistle: Just kidding.

Your approach to copying a registry key has made me do the same for my environment at school, allowing me to copy certain registry values to HKEY_LOCAL_USERS key, and on boot copy them to HKEY_LOCAL_MACHINE. This allows me to use AutoIt on any PC at school. ;)

Link to comment
Share on other sites

  • Moderators

Sorry for the lack of comments, I throw this together in about 30-45 minutes last night before in went to bed. I plan on rewriting the function to allow copying either a key or a value. I will also get rid of the $sRegHive variable as there is no need for it.

Anyhow glad you found it useful.

Link to comment
Share on other sites

  • 3 years later...

Sorry for the lack of comments, I throw this together in about 30-45 minutes last night before in went to bed. I plan on rewriting the function to allow copying either a key or a value. I will also get rid of the $sRegHive variable as there is no need for it.

Anyhow glad you found it useful.

There is a much easier way to do it using Windows reg.exe file.

http://commandwindows.com/reg.htm

C:\Windows\system32>reg copy /?

REG COPY KeyName1 KeyName2 [/s] [/f]

  KeyName    [\\Machine\]FullKey
    Machine  Name of remote machine - omitting defaults to the current machine.
             Only HKLM and HKU are available on remote machines.
    FullKey  ROOTKEY\SubKey
    ROOTKEY  [ HKLM | HKCU | HKCR | HKU | HKCC ]
    SubKey   The full name of a registry key under the selected ROOTKEY.

  /s         Copies all subkeys and values.

  /f         Forces the copy without prompt.

Examples:

  REG COPY HKLM\Software\MyCo\MyApp HKLM\Software\MyCo\SaveMyApp /s
    Copies all subkeys and values under the key MyApp to the key SaveMyApp

  REG COPY \\ZODIAC\HKLM\Software\MyCo HKLM\Software\MyCo1
    Copies all values under the key MyCo on ZODIAC to the key MyCo1
    on the current machine
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...