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()

Fixed it myself, partially.

Delete(current_key, value[0])

Now it seemingly leaves random values lingering, though, and I have to run it several times to actually clear the thing out.

Managed to fix the second issue by adding the items to a list and iterating that list to delete. And it's just occurred to me that that's because I was modifying the quantity and positions of the values as I was trying to enumerate them. I'm an idiot.

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)
    values = list()
    for i in range(4096):
        try:
            value = EnumValue(current_key, i)
        except:
            value = None
        if value:
            values.append(value[0])
    for i in values:
        DeleteValue(current_key, i)
    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()

Edited 4 Years Ago by oscargrower11

And the final script, since I figure it will be useful to someone.

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)
    values = list()
    for i in range(4096):
        try:
            value = EnumValue(current_key, i)
        except:
            value = None
        if value:
            values.append(value[0])
    for i in values:
        DeleteValue(current_key, i)
    CloseKey(current_key)

def delete_key(key):
    key = key.rstrip("\\")
    subkey = str()
    for letter in key[::-1]:
        if letter != "\\":
            subkey += letter
        else:
            break
    subkey = subkey[::-1]
    key = key[:(len(key) - len(subkey) - 1):1]
    current_key = OpenKey(HKEY_CURRENT_USER, key, 0, KEY_ALL_ACCESS)
    DeleteKey(current_key, subkey)
    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:
        current_key = scanned_subkeys.pop()
        delete_values(current_key)
        subkeys.append(current_key)
    subkeys.reverse()
    while len(subkeys) != 0:
        current_key = subkeys.pop()
        delete_key(current_key)


if __name__ == "__main__":
    main()
This question has already been answered. Start a new discussion instead.