Finding the registry for a key - JScript
Is there a way to search the Registry for a specific key using Windows Scripting Host?
I am using JavaScript (Jscript / VBScript?) To do this and the msdn library does not mention such a method: http://msdn.microsoft.com/en-us/library/2x3w20xf(v=VS.85).aspx
Thanks,
So here's an update to the problem:
The problem is a little more complicated than a straight search in the registry. I have to browse the installed products in the window to find the specific product entry that I want to remove. The registry path is defined as:
HKEY_LOCAL_MACHINE \ Software \ Microsoft ... \ Products.
The Products section lists the installed products, but their keys are identified as hash codes. Within product keys are other keys with specific names and specified values. I want to be able to search for the latest keys and values. How can I do this without going through unknown hash codes?
For example, I need to find a product with DisplayVersion key = 1.0.0. The path to this key:
HKLM \ Software \ Microsoft \ Windows \ CurrentVersion \ Installer \ UserData \ Products \ A949EBE4EED5FD113A0CB40EED7D0258 \ InstallProperties \ DisplayVersion.
How can I either take, or not write, the product key: A949EBE4EED5FD113A0CB40EED7D0258 ??
a source to share
Assuming you are using JScript through the Windows Scripting Host (not JavaScript from the browser), you can get the value of a specific key with WScript.RegRead
:
// MyScript.js
var key = 'HKEY_CURRENT_USER\\SessionInformation\\ProgramCount'
, wsh = WScript.CreateObject('WScript.Shell')
, val = wsh.RegRead(key);
WScript.Echo('You are currently running ' + val + ' programs.');
If you really need to search for a key or value based on some condition rather than a known registry key, you can implement your own recursive search algorithm where registry values of type "REG_SZ" are leaf nodes.
As an exercise to get more familiar with JScript on the Windows Scripting Host, I've made a little registry interface that does just that. The example included in the project shows how to perform a registry lookup in a WSF script like this:
<job id="FindDisplayVersions">
<script language="jscript" src="../registry.js"/>
<script language="jscript">
// Search the registry and gather 20 DisplayVersion values.
var reg = new Registry()
, rootKey = 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Installer\\UserData\\S-1-5-18\\Products'
, keyRegex = /Products\\(.*?)\\InstallProperties\\DisplayVersion$/
, valRegex = /^1\./
, maxResults = 20
, uids = [];
reg.find(rootKey, function(path, value) {
var keyMatch = keyRegex.exec(path);
if (keyMatch) {
if (valRegex.exec(value)) {
uids.push(keyMatch[1] + '\t=\t' + value);
if (uids.length >= maxResults) { return false; } // Stop searching
}
}
return true; // Keep searching.
});
WScript.Echo(uids.join("\n"));
</script>
</job>
Note that as @Robert Harvey points out, this can take a very long time if the root key is too deeply linked. Simple testing only takes a few seconds on the key of my choice, but your mileage may vary; of course, no guarantees or suitability for any purpose, don't blame me if your computer explodes.
a source to share