1

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!

user2338150
  • 479
  • 5
  • 14

2 Answers2

1

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"
)
zb226
  • 9,586
  • 6
  • 49
  • 79
  • It did occur to me, to write out files in tempfile, and use findstr, like constructs to merge respective entities to a common file. I'll post a solution once I write one. I've difficulty in findstr constructs. The inclusion in sub-directory is cool. Thanks again!:) – user2338150 Dec 27 '16 at 12:32
  • 1
    @user2338150: [`FINDSTR` is a beast](http://stackoverflow.com/questions/8844868/what-are-the-undocumented-features-and-limitations-of-the-windows-findstr-comman) best avoided, however in this case, the filtering required (skipping the header line) is sufficiently simple for `FINDSTR` to cope. – zb226 Dec 27 '16 at 13:17
0

This is what I wrote: It's basically similar to what was proposed by @zb226.

@echo off

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

ECHO Windows Registry Editor Version 5.00 > backup.reg

for /f "tokens=1,2" %%a in ('reg query HKLM\SOFTWARE\Classes /s /f "%1" 2^>NUL ^| findstr /I "^HKEY_"') do (
echo Deleting : %%a
reg export %%a bkp_tmp.reg /y >nul 2>&1
type bkp_tmp.reg | FINDSTR /V "Windows Registry Editor" >> backup.reg
reg delete %%a /f >nul 2>&1
)
del /f bkp_tmp.reg
goto :DLL_FOUND

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

It's ugly, as there definitely are key repetitions, but works for now. The same goes for reg delete operations. It's manageable for now, but definitely there could be a better solution.

user2338150
  • 479
  • 5
  • 14