Link to home
Start Free TrialLog in
Avatar of Tom Eason
Tom Eason

asked on

Delete Reg key only if empty via batch

I'm working on a batch file. What I want to do is delete a reg key if there are no other sub-keys

ex. "HKLM\Software\Vendor" is the key I want to delete. Don't delete it if "HKLM\Software\Vendor\Product" exists
ASKER CERTIFIED SOLUTION
Avatar of NVIT
NVIT
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tom Eason
Tom Eason

ASKER

Not entirely. In my example, I don't know what the 'product' key would be. I would have to be a wildcard. I know what the 'vendor' key is. In this instance it's Dell. I want to delete HKLM\Software\Dell only if there are no keys beneath that layer.
> I don't know what the 'product' key would be.

It doesn't matter what word it is. 'product' is just a word I used.

> I want to delete HKLM\Software\Dell only if there are no keys beneath that layer.

So, change it to...
set keyname=dell

Open in new window


Also, There's an error on line 6. Change the \\ to \
I made that change to set keyname and saw the \\.

The mistake I was making was not running it as Administrator. Once I did that it worked fine.

Thanks!
This line confused me at first, "REG QUERY "HKEY_LOCAL_MACHINE\Software\%keyname%" /s | find /i "%keyname%\" " Then I noticed the final '\' it made sense. This line was very helpful too "if !errorlevel! neq 0".
Glad you ve studied the code. Thanks for the update. There may be a better way to do it but this works i guess. Have a good one