Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Vista

Programming the WshShell Object : Working with Environment Variables

- Windows 10 Product Activation Keys Free 2019
- How to active Windows 8 without product key
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
3/4/2011 5:45:27 PM
Windows Vista keeps track of a number of environment variables that hold data such as the location of the Windows folder, the location of the temporary files folder, the command path, the primary drive, and much more. Why would you need such data? One example would be for accessing files or folders within the main Windows folder. Rather than guessing that this folder is C:\Windows, it would be much easier to just query the %SystemRoot% environment variable. Similarly, if you have a script that accesses files in a user’s My Documents folder, hard-coding the username in the file path is inconvenient because it means creating custom scripts for every possible user. Instead, it would be much easier to create just a single script that references the %UserProfile% environment variable. This section shows you how to read environment variable data within your scripts.

The defined environment variables are stored in the Environment collection, which is a property of the WshShell object. Windows Vista environment variables are stored in the "Process" environment, so you reference this collection as follows:

WshShell.Environment("Process")

Listing 1 shows a script that runs through this collection, adds each variable to a string, and then displays the string.

Listing 1. A Script That Displays the System’s Environment Variables
Set objWshShell = WScript.CreateObject("WScript.Shell")
'
' Run through the environment variables
'
strVariables = ""
For Each objEnvVar In objWshShell.Environment("Process")
strVariables = strVariables & objEnvVar & Chr(13)
Next
WScript.Echo strVariables

Figure 1 shows the dialog box that appears (your mileage may vary).

Figure 1. A complete inventory of a system’s environment variables.


If you want to use the value of a particular environment variable, use the following syntax:

WshShell.Environment("Process")("strName")

WshShellThe WshShell object
strNameThe name of the environment variable

Listing 1 shows a revised version of the script from this Listing to create a shortcut. In this version, the Environment collection is used to return the value of the %UserProfile% variable, which is used to contrast the path to the current user’s Desktop folder.

Listing 1. A Script That Creates a Shortcut File Using an Environment Variable
Set objWshShell = WScript.CreateObject("WScript.Shell")
strUserProfile = objWshShell.Environment("Process")("UserProfile")
Set objShortcut = objWshShell.CreateShortcut(strUserProfile & _
"\Desktop\Edit BOOT.INI.lnk")
With objShortcut
.TargetPath = "C:\Windows\Notepad.exe "
.Arguments = "C:\Boot.ini"
.WorkingDirectory = "C:\"
.Description = "Opens BOOT.INI in Notepad"
.Hotkey = "Ctrl+Alt+7"
.IconLocation = "C:\Windows\System32\Shell32.dll,21"
.WindowStyle = 3
.Save
End With
Other -----------------
- Programming the WshShell Object : Working with Registry Entries
- Programming the WshShell Object : Working with Shortcuts
- Programming the WshShell Object : Running Applications
- Programming the WshShell Object : Displaying Information to the User
- Programming the Windows Script Host : Programming the WScript Object
- Programming the Windows Script Host : Programming Objects
- Programming the Windows Script Host : Scripts and Script Execution
- Getting to Know the Windows Vista Registry - Finding Registry Entries
- Getting to Know the Windows Vista Registry - Working with Registry Entries
- Getting to Know the Windows Vista Registry - Keeping the Registry Safe
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Popular tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 windows Phone 7 windows Phone 8
programming4us programming4us
Celebrity Style, Fashion Trends, Beauty and Makeup Tips.
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server