I've been strugglin with this all day. I'm trying to use python to walk through a registry key and delete all the values it finds. I've succeeded in getting it to find all the nested values, but as soon as I try to delete one, I'm met with "WindowsError: [Error 2] The system cannot find the file specified."
I'm sure it's something simple I'm doing wrong, but I can't for the life of me figure out what it is.
from _winreg import *
def find_subkeys(key, subkeys, scanned_subkeys):
current_key = OpenKey(HKEY_CURRENT_USER, key, 0, KEY_ALL_ACCESS)
for i in range(4096):
try:
subkey = EnumKey(current_key, i)
except:
subkey = None
if subkey:
subkey = r"%s\%s" % (key, subkey)
if not subkey in subkeys and not subkey in scanned_subkeys:
subkeys.append(subkey)
scanned_subkeys.append(key)
CloseKey(current_key)
def delete_values(key):
current_key = OpenKey(HKEY_CURRENT_USER, key, 0, KEY_ALL_ACCESS)
for i in range(4096):
try:
value = EnumValue(current_key, i)
except:
value = None
if value:
value_to_delete = r"%s\%s" % (key, value[0])
print value_to_delete
DeleteValue(HKEY_CURRENT_USER, value_to_delete)
CloseKey(current_key)
def main():
subkeys = list(["Software\CUSpider\Spider3"])
scanned_subkeys = list()
while len(subkeys) != 0:
find_subkeys(subkeys.pop(), subkeys, scanned_subkeys)
scanned_subkeys.sort(lambda x,y: cmp(len(x), len(y)))
while len(scanned_subkeys) != 0:
delete_values(scanned_subkeys.pop())
if __name__ == "__main__":
main()