Q:

How to delete a registry key only if it has no values or subkeys using a *.bat file?

Problem is that I want to delete registry key only if no subkeys or values are present and I want to accomplish this using *.bat file. I have not found any viable solution. Anyone has any ideas?

A:

There may be a way to do this with Windows system commands, but your fastest route may be to write a simple console application to accept a key as an argument and then check for child nodes, removing those without any.  Then call it from the batch file.  If this is feasible, I can post some C# code to do the trick.
EDIT
Here is simple C# code for a console program which can be run to delete a key, as long as it is empty.  It uses HKEY_CURRENT_USER but can easily be modified to access HKEY_LOCALMACHINE as well.
using Microsoft.Win32;

namespace RegistryKeyDelete
{
   class Program
   {
      static int Main(string[] args)
      {
         const int EXIT_SUCCESS = 0;
         const int EXIT_FAILURE = 1;

         if (args.Length < 1)
            return EXIT_FAILURE;

         RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(args[0]);
         if (registryKey == null || registryKey.SubKeyCount != 0 || registryKey.ValueCount != 0)
            return EXIT_FAILURE;

         try {
            Registry.CurrentUser.DeleteSubKey(args[0], true);
         } catch {
            return EXIT_FAILURE;
         }

         return EXIT_SUCCESS;
      }
   }
}

Build it as RegistryKeyDelete and use it like this in the batch file:
RegistyKeyDelete Software\MyEmptyKey

If you want it to walk and parse the registry under a path, you can start with the above code and then enumerate the keys and check each one.  I'm assuming you already know which keys you are checking.
BTW - Standard disclaimer when doing anything with the registry: use caution and backup.

