Sign in to follow this  
Followers 0
big_daddy

_RegCopyKey()

6 posts in this topic

$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

Share this post


Link to post
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. ;)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Please refrain from reviving 3 year old threads.


[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

Share this post


Link to post
Share on other sites

I prefer mine anyway.

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  
Followers 0