Scanning through Windows registry...
Unknown Hero
unknown_hero007 at hotmail.com
Fri May 9 05:51:13 EDT 2008
On 7 touko, 14:25, Unknown Hero <unknown_hero... at hotmail.com> wrote:
> I'll post my code here once I have time to write it, currently I'm
> rather busy. That is merely for optimization suggestions and for
> others who might need the same sort of code I did.
>
> Thank you once again.
Finally managed to get it to work (heh, I was pretty darn lost even
though I had the best help) but as promised, I'll post my code here
for those who might be interested in it. The biggest of thanks to Tim
Golden, who basically walked me step-by-step through this. Most of the
code is copied from his examples above.
<code snippet>
#Goes through all keys and subkeys in the selected hive (defined as
root) and replaces the value 'old' with the value 'new'
#
#IMPORTANT! You should always back up the registry before attempting
to modify it.
#The author of this script CANNOT BE HELD RESPONSIVE for any damage
caused by running this script.
#
#To customize the script to your liking, you can alter the values old,
new, root.
#
#old and new can be any string value
#root has to be one of the following:
#
# _winreg.HKEY_LOCAL_MACHINE
# _winreg.HKEY_CURRENT_USER
# _winreg.HKEY_CLASSES_ROOT
# _winreg.HKEY_USERS
# _winreg.HKEY_CURRENT_CONFIG
import _winreg
HIVES = {
"HKEY_LOCAL_MACHINE" : _winreg.HKEY_LOCAL_MACHINE,
"HKEY_CURRENT_USER" : _winreg.HKEY_CURRENT_USER,
"HKEY_CLASSES_ROOT" : _winreg.HKEY_CLASSES_ROOT,
"HKEY_USERS" : _winreg.HKEY_USERS,
"HKEY_CURRENT_CONFIG" : _winreg.HKEY_CURRENT_CONFIG
}
old = "This was value before"
new = "This is new value"
start = "HKEY_LOCAL_MACHINE\\"
startHandle = _winreg.HKEY_LOCAL_MACHINE
#Values for start and startHandle are: HKEY_LOCAL_MACHINE,
HKEY_CURRENT_USER, HKEY_CLASSES_ROOT, HKEY_USERS, HKEY_CURRENT_CONFIG
class RegKey:
def __init__ (self, name, key):
self.name = name
self.key = key
def __str__ (self):
return self.name
def walk (top):
"""walk the registry starting from the key represented by
top in the form HIVE\\key\\subkey\\..\\subkey and generating
key, subkey_names, values at each level.
key is a lightly wrapped registry key, including the name
and the HKEY object.
subkey_names are simply names of the subkeys of that key
values are 3-tuples containing (name, data, data-type).
See the documentation for _winreg.EnumValue for more details.
"""
try:
if "\\" not in top: top += "\\"
root, subkey = top.split ("\\", 1)
# print "KEY:", root + "\\" + subkey
key = _winreg.OpenKey (HIVES[root], subkey, 0, _winreg.KEY_READ |
_winreg.KEY_SET_VALUE)
subkeys = []
i = 0
while True:
try:
subkeys.append (_winreg.EnumKey (key, i))
i += 1
except EnvironmentError:
break
values = []
i = 0
while True:
try:
values.append (_winreg.EnumValue (key, i))
i += 1
except EnvironmentError:
break
yield RegKey (top, key), subkeys, values
for subkey in subkeys:
for result in walk (top + "\\" + subkey):
yield result
except WindowsError:
# print 'Could not open key', root + "\\" + subkey + "\n"
pass
basickeys = []
i = 0
while True:
try:
basickeys.append (_winreg.EnumKey (startHandle, i))
i += 1
except EnvironmentError:
print i, 'subkeys found!'
break
for x in range(len(basickeys)):
for key, subkey_names, values in walk (start + basickeys[x]):
# print key
for (name, data, type) in values:
# print " ", name, "=>", data
if type == _winreg.REG_SZ and old in data:
_winreg.SetValueEx (key.key, name, 0, type, data.replace (old,
new))
</code>
Not the most effecient code, I'm sure, but it does what I want it to
do :D
Thank you once more, Tim. Also, thank you, Mike, for your advice on
saving the registry. I would have forgotten to note backing up the
registry in the beginning if it wasn't for you bringing that up. :D
More information about the Python-list
mailing list