Search code examples
windowsbatch-fileregistry

Backup returned keys from reg query to file in batch


I want to be able to export key-values of registry keys as returned by reg query. I'm trying to write a script to find registration for a particular dll and then write all keys to a backup file, before trying to achieve uninstall by deleting the keys. Here's what I could come up with so far:

@echo off

reg query HKLM\SOFTWARE\Classes /s /f %1 2>&1 >NUL
if errorlevel 1 goto DLL_MISSING

for /f "tokens=1,1" %%a in ('reg query HKLM\SOFTWARE\Classes /s /f %1 2^>NUL ^| findstr /I "^HKEY_"') do (
echo %%a
REG export %%a Backup.REG
)
goto :DLL_FOUND

:DLL_MISSING
echo Assembly not found.
goto :eof
:DLL_FOUND
echo Assembly found.

Right now reg export prompts to overwrite file, which I want append instead. How can I achieve the same? Also, please do suggest if there is some better way to automate uninstall duplicate(?) installs as installed by 'regasm'. I could prefer batch-file based solution instead of Powershell or something else. Thanks!


Solution

  • reg.exe does not support appending/combining of several exported keys. The easiest workaround seems to be to output each key's data into a separate file, and then merge these into a single file afterwards. Note that you need to make sure that the output key file is not picked up by the FOR loop, which I ensured by simply placing the combined key file in a subfolder called target.

    @ECHO OFF   
    
    MKDIR target
    ECHO Windows Registry Editor Version 5.00 > target\combined.reg
    
    FOR %%G IN (*.reg) DO (
        TYPE "%%G" | FINDSTR /V "Windows Registry Editor" >> target\combined.reg
        DEL "%%G"
    )