Possible batch export different Windows registry keys into a single .reg file? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
cadudesun
Posts: 129
Joined: 04 Jun 2016, 10:26

Possible batch export different Windows registry keys into a single .reg file?

24 Sep 2021, 11:34

Hi, I'd appreciate your help.

I use successfully the script below to export a single registry key to a .reg file:

Code: Select all

key = HKEY_CURRENT_USER\Software\NameOfApplication
out = %A_ScriptDir%\NameOfApplication.reg
FileRecycle, %out%
RunWait, %A_WinDir%\System32\reg.exe export "%key%" "%out%,, HIDE
I tried to adapt this script to merge two different registry keys into a single .reg file. However there was an AHK error.
This is the draft idea:

Code: Select all

key = HKEY_CURRENT_USER\Software\NameOfApplication1
key = HKEY_CURRENT_USER\Software\NameOfApplication2
out = %A_ScriptDir%\ApplicationKeysMerged.reg
FileRecycle, %out%
RunWait, %A_WinDir%\System32\reg.exe export "%key%" "%out%,, HIDE
=> Does anyone know how to improve the script to it work properly?

Thank you very much!
Cadu
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: Possible batch export different Windows registry keys into a single .reg file?

24 Sep 2021, 11:55

You could create the REG files, use FileRead to read the text, FileAppend to a new file, and omit the duplicate file header; or just append one file's text to the other file.
cadudesun
Posts: 129
Joined: 04 Jun 2016, 10:26

Re: Possible batch export different Windows registry keys into a single .reg file?

24 Sep 2021, 12:20

mikeyww wrote:
24 Sep 2021, 11:55
You could create the REG files, use FileRead to read the text, FileAppend to a new file, and omit the duplicate file header; or just append one file's text to the other file.
Thanks for your reply @mikeyww
Considering the FileRead and FileAppend commands to accomplish the task, would it be possible to use a simple syntax that I don't need to type each .reg file name?

For instance, given the following .reg outputs:
C:\NameOfApplication1.reg
C:\NameOfApplication2.reg
C:\NameOfApplication3.reg

Could FileRead be used with a wildcard like "%A_ScriptDir%\FileReadNameOfApplication*.reg", so that all three files would be read through a single command?
If possible, could you please share a syntax of FileRead/FileAppend working that way?

Thank you very much!
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: Possible batch export different Windows registry keys into a single .reg file?  Topic is solved

24 Sep 2021, 14:28

Code: Select all

dir     = %A_ScriptDir%
out     = %dir%\new.reg
header  = Windows Registry Editor
newText = %header% Version 5.00`n
FileRecycle, %out%
Loop, Files, %dir%\*.reg
{ FileRead, text, %A_LoopFilePath%
  newText .= Instr(text, header) = 1 ? RegExReplace(text, "^.+?\r\n") : text
}
FileAppend, %newText%, %out%
Run, notepad.exe "%out%"
cadudesun
Posts: 129
Joined: 04 Jun 2016, 10:26

Re: Possible batch export different Windows registry keys into a single .reg file?

24 Sep 2021, 14:43

Many thanks for sharing the full syntax @mikeyww 👍
It's the first time I'm using "FileRead" and "FileAppend" and without your help I'd never accomplish exporting different Windows registry keys into a single .reg

The syntax worked as expected, however there is just a minor issue:
The content of the read files are being duplicated (appearing twice) inside the output "new.reg"
Would you know how to fix that?
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: Possible batch export different Windows registry keys into a single .reg file?

24 Sep 2021, 14:49

The script does not check for duplicated keys among multiple files. You would need to add code to do that, if that is the issue that you are describing. It would take a bit of work because a duplicated line might be part of a different registry key. You would learn some AHK in the process! On the other hand, a registry file with duplicated lines or sections typically has no adverse effects, as far as I know.
cadudesun
Posts: 129
Joined: 04 Jun 2016, 10:26

Re: Possible batch export different Windows registry keys into a single .reg file?

24 Sep 2021, 21:03

mikeyww wrote:
24 Sep 2021, 14:49
The script does not check for duplicated keys among multiple files.
Thanks for the reply!
Actually, there is no duplicated key in the input .reg files that are being read/appended.
The input files are good as they are, and there is no need for for checking duplicated keys among multiple files.
The duplication seems to be happening when the read/append script creates the output "new.reg"
So each single key that exists once in the input .reg files can be seen twice in "new.reg".
Is there any part of the read/append script that could potentially create duplicates only in the "new.reg" output?
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: Possible batch export different Windows registry keys into a single .reg file?

24 Sep 2021, 21:48

I don't think so, but you can post the complete, exact script that you are using, along with the other files.
cadudesun
Posts: 129
Joined: 04 Jun 2016, 10:26

Re: Possible batch export different Windows registry keys into a single .reg file?

25 Sep 2021, 06:23

Hi @mikeyww
Thanks for your availability to check the actual script and output, which are available for direct download here (zip): https://bit.ly/3i4jfXW
I also recorded my screen detailing everything I performed and when duplicates are noticed: https://bit.ly/3zHcPUA
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: Possible batch export different Windows registry keys into a single .reg file?

25 Sep 2021, 06:50

Got it. Our paths crossed earlier. You copied my first script, I caught the bug and edited the post, and you ran the old version. The latest (above) should work.

The bug: the script created a .reg file. When the file loop was run, the loop identified its own nascent .reg file, and copied its contents into itself.
The fix: the .reg file is created after the loop is completed.
cadudesun
Posts: 129
Joined: 04 Jun 2016, 10:26

Re: Possible batch export different Windows registry keys into a single .reg file?

25 Sep 2021, 16:07

Understood. Script tested now and it's working perfectly.
Many thanks for your support and attention mikeyww!
I learned a lot from this thread 👍

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: TAC109 and 257 guests