How to write the registry in VBScript _vbs

Source: Internet
Author: User
Category: Programs
As users of the DOS system were aware earlier in the year, MS-DOS operating systems allow ordinary users to write batch files to achieve simple programming. It effectively simplifies our work and brings us a lot of convenience. There were even programmers who wrote batch files.

In fact, in addition to providing an Easy-to-use interface, the operating system should have a built-in script (scripting) language in order to be complete. In some ways, batch files can be said to be the scripting language of MS-DOS. In the Windows age, although the Windows system still retains the batch processing, the batch processing in Windows is too simple to meet the needs of users-such as modifying the registry, batch processing can be done? In fact, after Windows 98, Windows system, in addition to retaining the original batch system, began to add support for scripting languages, which is "Windows Scripting Host", the Chinese translation of "Windows Script Host", referred to as WSH.

WSH itself is not a scripting language, it is the operating environment of the scripting language, WSH Support scripting language has JScript (Microsoft version of JavaScript, and true JavaScript is not the same) and vbscript--this dongdong everyone must be familiar with, the Web page is commonly used.

The WSH script engine (scripting Engine) has two ways, one is the CScript.exe of the command, and the other is the WScript.exe of Windows. The Scripting Programs we write (plain text files with the extension ". js" and ". vbs") must be loaded to run by one of these two.

Although WSH supports the JScript and VBScript languages, it still works best with some of the objects provided by the WSH itself, as well as the object's properties and methods (method). Programming about JScript and VBScript we don't have much to say here. We're still focusing on the WSH object. In fact, the registry programming involved in this article requires virtually no knowledge of VBScript, and we simply need to apply ready-made statements.

The main objects in WSH are: Wscirpt, wsharguments, WshShell, Wshurlshortcut, WSHNetwork, Wshspecialfolders, Wshcollection, Wshenvironment, Wshshort and so on.

Each object has its own properties and methods. Here we simply introduce the methods and properties of the objects involved in accessing the registry.

The first is the WshShell object. This object can be used to set system environment variables and modify registry data. To modify the registry data, you need to use three methods of the WshShell object: RegDelete (delete registry data), RegWrite (write or create new registry data), and RegRead (read registry data).

Usually we only need to use the first two methods, their specific use is as follows:

One, RegWrite (write or create new registry data)

* New Child primary key

Syntax is: WshShell.RegWrite "Child primary Key Name"

For example, we want to create a new subkey, "Hkey_current_usermyreg," using the following statement:

WshShell.RegWrite "Hkcumyreg"

Note: The subkey must end with a backslash (), if you accidentally forget to enter this (), the method returns the key value, which means you want to create a key value called Myreg under HKEY_CURRENT_USER. This must be very careful!

Note quotation marks the beginning of a primary key must be one of the following root key names:



HKEY_CURRENT_USER (can be abbreviated as HKCU), HKEY_LOCAL_MACHINE (HKLM), HKEY_CLASSES_ROOT (HKCR), HKEY_USERS and Hkey_current_config
* Create a new key value under the subkey (or overwrite the data of an existing key value)

Syntax is: WshShell.RegWrite "subkey key Value name", "Data of key value", "type of key value"

For example, we want to create a new string key value KeyValue under the subkey "Hkey_current_usermyreg", set the data for the key value to "str", and use the following statement:

WshShell.RegWrite "Hkcumyregkeyvalue", "str"

(Note: The key value is a string value, you can omit the "key value type" declaration)

If you build a binary or DWORD value and the data is "1", you must also declare the type of the key value, as follows:

WshShell.RegWrite "Hkcumyregkeyvalue", 1, "REG_Binary"

WshShell.RegWrite "Hkcumyregkeyvalue", 1, "REG_DWORD"

Note that the data for binary and DWORD values cannot be quoted, and the data for string values must be quoted.

Second, RegDelete (delete registry data)

* Delete a child primary key

Syntax is: wshshell.regdelete "Child primary Key Name"

For example, we want to delete the child primary key "Hkey_current_usermyreg" by using the following statement:

Wshshell.regdelete "Hkcumyreg"

* Delete a key value for a child primary key

Syntax is: wshshell.regdelete "Child primary Key Name key value name"

For example, we want to delete the key value keyvalue of the child primary key "Hkey_current_usermyreg" by using the following statement:

Wshshell.regdelete "Hkcumyregkeyvalue"

Similar to RegWrite, there is a "" sign that deletes a child primary key, and no "" indicates that the key value under the child primary key is being deleted.
In addition to WshShell objects, we must also understand the Wscirpt object. The Wscirpt object represents the scripting Engine and automatically generates this object whenever the Engine is started. The WScript object provides methods for creating and reading objects. To use WSH other objects, such as WshShell objects, you must first create and read with the WScript object's methods (CreateObject, GetObject).

The syntax for creating an object is as follows:

WScript.CreateObject (Strprogid)

Where Strprogid is the identity name of the object we are going to create.

For example, to use the WshShell object and its properties and methods, we first need to create a WshShell object using the WScript object's method CreateObject, as follows:

Set WshShell = WScript.CreateObject ("Wscript.Shell")

Give me an example. As you know, if you use Run on the Start menu, Windows will record your "crime" Trace in the list box. In fact, this data is recorded in the registry hkey_current_usersoftwaremicrosoftwindows

Currentversionexplorerrunmru subkey, we just delete this subkey, and then re-establish, is it OK?

Now we can write a script. In the case of VBS, we can create a new file with Notepad, and then enter (where "//" after the text as a note, do not have to enter):

To define the object, to edit the registry, we need to use the WshShell object and its methods

Dim WshShell

object to create a WshShell object by CreateObject the method of the

Set WshShell = WScript.CreateObject ("Wscript.Shell")

Then we use the WshShell object's method RegDelete to remove the Hkey_current_usersoftwaremicrosoft

Windowscurrentversionexplorerrunmru Child keys

Wshshell.regdelete "Hkcusoftwaremicrosoft

Windowscurrentversionexplorerrunmru "

Finally, we restore the primary key and restore the string value "MRUList" under the primary key, setting its data to an empty string

WshShell.RegWrite "Hkcusoftwaremicrosoft

Windowscurrentversionexplorerrunmrumrulist "," "

This is the end of the program, we can save the file as a cleanmru.vbs. Now we can use scripting engine to execute this program. Assuming we've just saved this file in D:temp, we can use the "run"--wcript.exe d:tempcleanmru.vbs on the Start menu. Reboot, what? "Run" is already empty!

If we are so painstaking to compile the program, and finally have to rely on hand to run, it is better to use the Registry Editor every time to operate it! We can actually make it load automatically at every boot. So, we must all know that--we can use Registry Editor, in the HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows

Currentversionrun creates a string value "Cleanmru" under the subkey, setting its data to "Wcript.exe d:tempcleanmru.vbs". Hey, that's it. Every time you turn on, Windows automatically executes the script to clear the history of run.

Limited to space, this article only introduces the initial use of the object of the WSH registry modification. As long as we add some simple statements to the script, we can easily interact with each other-and, of course, we need more knowledge about WSH and VBScript. If you are interested, you can download the Help documentation for WSH and VBScript to the Microsoft Web site, which is http://www.microsoft.com/china/scripting/windowshost/ Wshdoc.exe and Http://www.microsoft.com/china/scripting/vbscript/download/vbsdoc.exe (both Chinese). In addition, in the Windows Samples directory, there is a WSH folder, there are a lot of. vbs and. JS routines, you can open to see (right mouse click icon, choose "Edit"), I believe there will be a lot of harvest.

Finally, if the Windows Scripting Host is already installed on your machine, and the VBS program is still not running, it is likely that you or another application has modified it. The Association of the VBS. As far as I know, almost every machine is equipped with "Super Jie Ba" will be modified. The Association of the VBS file. It's all right, you can just remove it from the Control Panel-Add/Remove Programs-Windows Installer-"accessories"-"details" and then reload "Windows Scripting Host".

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.