Retrieves the code for a series of commands typed in the Run dialog box with the VBS _VBS

Source: Internet
Author: User
Ask:
Hello, Scripting Guy! Sometimes I type multiple commands in the Run dialog box and then I want to retrieve them. I know that the commands I've used recently are cached somewhere, because when I start typing in the Run dialog box, they are displayed. How do I use scripts to retrieve these commands?
--KJ
For:
Hello, KJ. You know, the first thing we think about when you see your problem is: Why didn't we think about it? Needless to say, the Scripting Guys have been using the Run dialog box for years, and we know very well that the most recently used commands (26 of the most recently used) are cached somewhere on your computer, if you've counted them. However, we have never written a script that retrieves this list. How can we overlook something so obvious?
Note: In fact, it is not surprising that we have overlooked something so obvious. So far, for example, the Scripting Guys have been in their current building for about a year, but just a few weeks ago, the Scripting Guys who wrote this column found stairs from their offices to the downstairs lobby.
After a little groping, we find that the information is stored in the registry, or rather, it is stored as a single registry value in the registry key Hkcu\software\microsoft\windows\currentversion\explorer\runmru. Isn't that a good thing? Of course it's good; after all, this allows us to write the following script:
Copy Code code as follows:

Const HKEY_CURRENT_USER = &h80000001
StrComputer = "."
Set objregistry = GetObject ("winmgmts:\\" & StrComputer & "\root\default:StdRegProv")
strKeyPath = "Software\microsoft\windows\currentversion\explorer\runmru"
Objregistry.enumvalues HKEY_CURRENT_USER, strKeyPath, arrValueNames, arrValueTypes
For each strvalue in arrValueNames
If Len (strvalue) = 1 Then
Objregistry.getstringvalue Hkey_current_user,strkeypath,strvalue,strruncommand
Intlength = Len (Strruncommand)
Strruncommand = Left (Strruncommand, intLength-2)
WScript.Echo Strruncommand
End If
Next
The script connects to the RunMRU item, and then enumerates the values for all the values found here. (Yes, we know: The value of the value?) This is the interesting thing about the registry terminology. To implement this feature, the script first defines a constant named HKEY_CURRENT_USER, and sets the value to &H80000001; will later use the constant to tell the registry hive to be processed by the script. We then connect to the WMI service on the local computer, and be sure to bind to the root\default namespace, the home directory of the WMI registry provider.
Note: Can we use this same script to retrieve the most recently used commands on a remote computer? Of course, you can simply assign the name of the remote computer to the variable strComputer.
After connecting to the WMI service, assign the value Software\microsoft\windows\currentversion\explorer\runmru to a variable named strKeyPath. Then use the EnumValues method to get a collection of all the registry values in the RunMRU item:
Objregistry.enumvalues HKEY_CURRENT_USER, strKeyPath, arrValueNames, arrValueTypes
As you can see, we passed four parameters to EnumValues:
Parameters
Description
HKey_Current_User
The registry hive in which information can be found.
strKeyPath
The path to the RunMRU item in the HKCU hive.
arrValueNames
This is an output parameter that is used as a location to store all registry value names. All we have to do is provide a variable name for EnumValues, and then EnumValues will populate this variable with all the value names in RunMRU.
arrValueTypes
Another output parameter that contains the data type corresponding to each value found in the RunMRU. This parameter is required, but because the data type of the value found in RunMRU is REG_SZ, we do not actually use it in the script.
As it turns out, each command you type in the Run dialog box has its corresponding value in the registry, and you assign names to those values by using the letters A through Z (which explains why only 26 of the most recently used commands in the registry are tracked). In the registry, RunMRU as shown in the following illustration:

After the EnumValues method is executed, we will return the collection of all the names of these values; in other words, our collection will consist of letters A through Z. Very good, except that the collection does not contain any actual commands. To get these commands (which is our ultimate goal), we need to connect to and read each of the 26 values in the registry.
Can we do this so that we can easily connect to 26 values in the registry and read each value? Of course you can; in fact, this is what the following code does:
Copy Code code as follows:

For each strvalue in arrValueNames
If Len (strvalue) = 1 Then
Objregistry.getstringvalue Hkey_current_user,strkeypath,strvalue,strruncommand
Intlength = Len (Strruncommand)
Strruncommand = Left (Strruncommand, intLength-2)
WScript.Echo Strruncommand
End If
Next
You're right: at first glance, it's a little scary, isn't it? To tell you why, let's introduce you to a simplified version of each loop, and then I'll explain why I added some additional code to this loop. The simplified cycle is as follows:
For each strvalue in arrValueNames
Objregistry.getstringvalue Hkey_current_user,strkeypath,strvalue,strruncommand
WScript.Echo Strruncommand
Next
All we have to do here is create a loop that loops through all the registry values. To read each of these values, we simply call the GetStringValue method:
Objregistry.getstringvalue Hkey_current_user,strkeypath,strvalue,strruncommand
The four parameters passed by GetStringValue: constant HKEY_CURRENT_USER, variable strKeyPath, variable strvalue (representing the name of each value, such as A, B, or C), and output parameters named Strruncommand. By using this output parameter, we simply specify a variable name, and the GetStringValue method assigns the value of the registry value (that is, the corresponding Run command) to it. After calling GetStringValue, we will echo back the Strruncommand, continue the loop, and process the next value in the collection.
A lot has been said about this simplified for Each loop, and what about all the extra code in the true for Each loop? The extra code is used primarily to provide us with a slightly better output. For example, in the RunMRU item, there is a registry value named MRUList. This does not represent an actual command, but rather represents the order in which the recently used commands occurred. This is not important to us (at least not today), so we'd rather skip the mrulist value. That's what the following code is doing:
If Len (strvalue) = 1 Then
In this line of code, we use the Len function to check the number of characters in the value name. If the number of characters (length) equals 1, we will proceed and read the value. If the length is not equal to 1 (which is obviously the case when MRUList has 7 characters), then we simply skip the value and move to the next item in the collection.
Another small piece of code that we added is:
Intlength = Len (Strruncommand)
Strruncommand = Left (Strruncommand, intLength-2)
If you view the registry, you will notice that a \1 is added to the end of all commands. If you want, you can keep it, but it's easy to get rid of it. All we have to do is determine the length of the command and then use the left function to return the first X character in the string. What is x equal to? It equals the total number of characters minus 2. This means that we're going to get all the characters except the last 2 characters (that is, \1) and echo them back to the screen.
You have now achieved your goal: a script that returns the most recently used command that you typed in the Run dialog box. We still don't know where the mysterious staircase leads, but we need to do something important first.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.