| 1 |
ninoborges |
8 |
## Test registry script. Because this code changes a unicode string in place Py2exe will prolly have to
|
| 2 |
|
|
## be compliled with the --packages=encodings flag.
|
| 3 |
|
|
|
| 4 |
|
|
from _winreg import *
|
| 5 |
|
|
#import _winreg
|
| 6 |
|
|
|
| 7 |
|
|
## EBorges
|
| 8 |
|
|
## Function: changeWinRegValue
|
| 9 |
|
|
## Purpose: This function will change 1 registry value in place based on a search and replace string.
|
| 10 |
|
|
## Arguments: rootHandle - one of the HKEY* constants.
|
| 11 |
|
|
## regKey - the key with the value you wish to replace
|
| 12 |
|
|
## valueName - the value name whose information you wish to change
|
| 13 |
|
|
## stringFind - a string to search for in the value's information
|
| 14 |
|
|
## stringReplace - the string it will replace stringFind with
|
| 15 |
|
|
def changeWinRegValue(rootHandle,regKey,valueName,stringFind,stringReplace):
|
| 16 |
|
|
iKeyObj = OpenKey(rootHandle, regKey)
|
| 17 |
|
|
result = QueryValueEx(iKeyObj,valueName)
|
| 18 |
|
|
iKeyObj.Close()
|
| 19 |
|
|
|
| 20 |
|
|
value = result[0]
|
| 21 |
|
|
type = result[1]
|
| 22 |
|
|
value = value.replace(stringFind,stringReplace)
|
| 23 |
|
|
oKeyObj = OpenKey(rootHandle, regKey,0,KEY_WRITE)
|
| 24 |
|
|
SetValueEx(oKeyObj,valueName,'',type,value)
|
| 25 |
|
|
oKeyObj.Close()
|
| 26 |
|
|
|
| 27 |
|
|
## Function: getRegInfo
|
| 28 |
|
|
## Purpose: This function will give you the value of a key pased into it.
|
| 29 |
|
|
## Arguments: handle - One of the HKEY* constants
|
| 30 |
|
|
## keyPath - The path to the key you want the value for.
|
| 31 |
|
|
## keyName - The name of the key you want the value of.
|
| 32 |
|
|
## Returns: result - The string value of the key
|
| 33 |
|
|
def getRegInfo(handle,keyPath,keyName):
|
| 34 |
|
|
keyObj = OpenKey(handle,keyPath)
|
| 35 |
|
|
result = QueryValueEx(keyObj,keyName)
|
| 36 |
|
|
keyObj.Close()
|
| 37 |
|
|
result = result[0]
|
| 38 |
|
|
return result
|
| 39 |
|
|
|
| 40 |
|
|
## Function: deleteRegKeyWithKeys
|
| 41 |
|
|
## Purpose: This function will allow the deletion of registry keys that have subkeys
|
| 42 |
|
|
## by deleting the subkeys of a registry key.
|
| 43 |
|
|
## This is currently a no-no from within Python, with good reason. So
|
| 44 |
|
|
## use with EXTREAM CAUTION!!
|
| 45 |
|
|
def deleteRegKeyWithKeys(handle,regKey,backUpDir):
|
| 46 |
|
|
# Enumerate the keys
|
| 47 |
|
|
subKeys = []
|
| 48 |
|
|
rKeyObj = OpenKey(handle,regKey)
|
| 49 |
|
|
numbSubKeys = QueryInfoKey(rKeyObj)[0]
|
| 50 |
|
|
for i in range (0,numbSubKeys):
|
| 51 |
|
|
subKeys.append(EnumKey(rKeyObj,i))
|
| 52 |
|
|
rKeyObj.Close()
|
| 53 |
|
|
# Delete the key and subkeys
|
| 54 |
|
|
wKeyObj = OpenKey(handle,regKey,0,KEY_WRITE)
|
| 55 |
|
|
for x in subKeys:
|
| 56 |
|
|
DeleteKey(wKeyObj,x)
|
| 57 |
|
|
wKeyObj.Close()
|
| 58 |
|
|
|
| 59 |
|
|
|
| 60 |
|
|
|
| 61 |
|
|
if __name__ == '__main__':
|
| 62 |
|
|
rootHandle = HKEY_CURRENT_USER
|
| 63 |
|
|
regKey = 'Software\\Commence\\Commence\\6.1\\windows\\1843951'
|
| 64 |
|
|
valueName = 'value_2'
|
| 65 |
|
|
stringFind = 'ric3'
|
| 66 |
|
|
stringReplace = 'ric1'
|
| 67 |
|
|
|
| 68 |
|
|
#changeWinRegValue(rootHandle,regKey,valueName,stringFind,stringReplace)
|
| 69 |
|
|
deleteRegKeyWithKeys(rootHandle,regKey) |