If reg key exists then delete it with batch?

April 26, 2010 at 01:27:41
Specs: Windows XP
Is it possible to delete a registry key only if it exists with a batch file?

For example, I would like to completely delete the following registry keys 'KeyName' without confirmation.

REG DELETE "HKCU\Software\CompanyName\ProductName\KeyName" /F
REG DELETE "HKLM\SOFTWARE\CompanyName\ProductName\KeyName" /F
REG DELETE "HKCU\Software\Wow6432Node\CompanyName\ProductName\KeyName" /F
REG DELETE "HKLM\SOFTWARE\Wow6432Node\CompanyName\ProductName\KeyName" /F

The above works in simply deleting the keys.
Is it possible to attempt such deleting only if each key exists? Also, if the key exists is deleted is it possible to echo a success message for that key?

Basically, I would like a batch to only delete a key if it exists without confirmation then echo a success message for that deleted key.

Thanks in advance for any help.


See More: If reg key exists then delete it with batch?

Report •


#1
April 26, 2010 at 03:17:05
You can issue a REG QUERY for the key then if ErrorLevel 0 issue the REG DELETE. E.G:

reg query KEY
if not ErrorLevel 1 (
  reg delete KEY
) else (
  echo. KEY not found
) 


Report •
Related Solutions


Ask Question