Windows XP Annoyances Free Open Book

Windows XP Annoyances

Previous PageNext Page

9.3 Accessing the Registry from Scripts

The following code is used to write, read, and delete information in the Registry. Include the following three routines in your script:

Sub RegistryWrite(KeyName, ValueName, ValueData, ValueType)
  ValueType = UCase(ValueType)
  If ValueType <> "REG_DWORD" and ValueType <> "REG_BINARY" Then _
                                                   ValueType = "REG_SZ"
  Set WshShell = WScript.CreateObject("WScript.Shell")
  WshShell.RegWrite KeyName & "\" & ValueName, ValueData, ValueType
End Sub

Function RegistryRead(KeyName, ValueName)
  Set WshShell = WScript.CreateObject("WScript.Shell")
  RegistryRead = WSHShell.RegRead(KeyName & "\" & ValueName)
End Function

Sub RegistryDelete(KeyName, ValueName)
  Set WshShell = WScript.CreateObject("WScript.Shell")
  WshShell.RegWrite KeyName & "\" & ValueName, ""
  WshShell.RegDelete KeyName & "\" & ValueName
End Sub

Using these three routines, you can accomplish nearly all Registry tasks. To create a Registry key, type this (note that all HKEY... roots must appear in uppercase):

Call RegistryWrite("HKEY_LOCAL_MACHINE\Software\My Key", "", "", "")

To assign data to a Registry value:

Call RegistryWrite("HKEY_LOCAL_MACHINE\Software\My Key", "My Value", _ 
                                                   "Some Data", "")

Leave "My Value" blank to set the (default) value. To read the data stored in a given value:

Variable = RegistryRead("HKEY_LOCAL_MACHINE\Software\My Key", "My Value")

Leave "My Value" blank to read the (default) value. To delete a key:

Call RegistryDelete("HKEY_LOCAL_MACHINE\Software\My Key", "")

To delete a value:

Call RegistryDelete("HKEY_LOCAL_MACHINE\Software\My Key", "My Value")

To delete the (default) value in a key, we just set the value to nothing:

Call RegistryWrite("HKEY_LOCAL_MACHINE\Software\My Key", "", "", "")

You'll notice that, in the RegistryDelete subroutine, there's a RegWrite statement. This is necessary to ensure that the key or value that you're trying to delete actually exists. If you don't include this statement and try to delete a nonexistent key or value from the Registry, the Windows Script Host will give an error to the effect that "The system cannot find the file specified." (A helpful Microsoft error message, as always.) This way, the subroutine will create the key or value entry to be deleted if it doesn't already exist.

 

As part of a security/safety feature present in Windows XP (and Windows 2000), you won't be able to delete a key that contains subkeys (this is not true of Windows 9x/Me) using the RegistryDelete routine. See Section 3.2.1 in Chapter 3 for a workaround using Registry patch files.

See Chapter 3 for more information on Registry keys and values.

 

Previous PageNext Page


     Main Menu
Cover
Copyright
Preface
Chapter 1. Making the Most of Windows XP
Chapter 2. Basic Explorer Coping Skills
Chapter 3. The Registry
Chapter 4. Tinkering Techniques
Chapter 5. Maximizing Performance
Chapter 6. Troubleshooting
Chapter 7. Networking and Internetworking
Chapter 8. User Accounts and Administration
Chapter 9. Scripting and Automation
9.1 Building a Script with VBScript
9.2 Running Applications from Scripts
9.3 Accessing the Registry from Scripts
9.4 Manipulating Files from Scripts
9.5 Creating Windows Shortcuts and Internet Shortcuts in Scripts
9.6 Networking with Scripts
9.7 Manipulating Internet Explorer from Scripts
9.8 Using Command-Line Parameters in Scripts
9.9 Managing Services with Scripts
9.10 Writing CGI Scripts for a Web Server
9.11 Making a Startup Script
9.12 Deciphering Script Errors
9.13 Finding a Better Editor
9.14 Further Study
9.15 Automating Scripts with Scheduled Tasks
9.16 Wacky Script Ideas
Chapter 10. Installing Windows XP
Appendix A. Setting Locator
Appendix B. BIOS Settings
Appendix C. Command Prompt Crash Course
Appendix D. TCP/IP Ports
Appendix E. Error Messages (Blue Screen of Death)
Colophon


More Books
PHP Hacks
Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
The Koran (Holy Qur'an)
Macromedia Flash 8 Bible
Search Engine Optimization for Dummies
YouTube Traffic
PHP 5 for Dummies
Harry Potter and The Chamber of Secrets
Harry Potter and the Sorcerer's Stone
The Pilgrim's Progress
Wireless Hacks
Flash Hacks. 100 Industrial-Strength Tips & Tools
PayPal Hacks. 100 Industrial-Strength Tips and Tools
Amazon Hacks
Pdf Hacks
The Da Vinci Code
Google Hacks
The Holy Bible
Windows XP For Dummies
Harry Potter and the Half-Blood Prince
Seo Book
Upgrading and Repairing Networks
Macromedia Dreamweaver 8 UNLEASHED
Windows XP Annoyances
Windows XP Hacks
Microsoft Windows XP Power Toolkit
Teach Yourself MS Office In 24Hours
iPod & iTunes Missing Manual
PC Hacks 100 Industrial-Strength Tips and Tools
PC Overclocking, Optimization, and Tuning - 2th Edition
PC Hardware In A Nutshell 3rd Edition
PC Hardware in a Nutshell, 2nd Edition
Upgrading and Repairing PCs
Google for Dummies
MySQL Cookbook
Teach Yourself Macromedia Flash 8 In 24 Hours
PHP CookBook
Sams Teach Yourself JavaScript in 24 Hours
PHP5 Manual
Free Games Paper Airplanes
500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele