environment variables: wshshell.environment or wshshell.expandenv ironmentstrings? 
Author Message
 environment variables: wshshell.environment or wshshell.expandenv ironmentstrings?

There are two ways to acces environment variables in WSH.
These examples both return the 'Logonserver'-variable:
- Set WshSysEnv =WshShell.Environment("PROCESS")
  WScript.Echo WshSysEnv("LOGONSERVER")
- WScript.Echo WshShell.ExpandEnvironmentStrings("%LOGONSERVER%")

I presume there must be a reason for the existence of two methods to
accomplish the same task.
What is the difference between them, and when should you use them?

Geert VC



Sat, 19 Oct 2002 03:00:00 GMT  
 environment variables: wshshell.environment or wshshell.expandenv ironmentstrings?


Fri, 19 Jun 1992 00:00:00 GMT  
 environment variables: wshshell.environment or wshshell.expandenv ironmentstrings?

Quote:

>There are two ways to acces environment variables in WSH.
>These examples both return the 'Logonserver'-variable:
>- Set WshSysEnv =WshShell.Environment("PROCESS")

You have declared a VBA collection object in WshSysEnv. If
you knew nothing about the Environment before hand, you could
use this object to get both the Names and Values of your Env.
variables. You will have to do some parsing to get them separately
because a VBA collection doesn't give a *key array* as in a Perl
hash(associative array) or a Dictionary Object.

' begin code
dim oEnv, WshShell
set WshShell = WScript.CreateObject("WScript.Shell"  )
Set oEnv =WshShell.Environment("PROCESS")
dim zzItem, sOut
for each zzItem in oEnv
' if necessary, split zzItem on *=* to separate the names and values
sOut = sOut & zzItem & vbCrLf
next
WScript.echo sOut
' end code

Quote:
>  WScript.Echo WshSysEnv("LOGONSERVER")
>- WScript.Echo WshShell.ExpandEnvironmentStrings("%LOGONSERVER%")

This method inputs a string and returns a string.

Quote:

>I presume there must be a reason for the existence of two methods to
>accomplish the same task.
>What is the difference between them, and when should you use them?

One is a simple helper function ( Ex Env Str Method) and the other is a
powerfull ( although slightly crippled ) VBA collection object ( Env Property).

In this case use the VB or VBA Object Browser and look at the return
values. When an Object is returned, then you have access to another set
of methods and  properties.

HTH,
    Mark Pryor

Quote:

>Geert VC



Sat, 19 Oct 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. WSHShell.Environment?

2. WshShell.Environment() - script optimization

3. Can some explain how the WshShell.Environment works??

4. WshShell.Run vs WshShell.Exec

5. runas with WshShell.Exec versus WshShell.Run

6. Using variables in WshShell.Run ("....")

7. Using a variable within WshShell object and Run method (newbie at work)

8. WshShell.Run: Spaces in variable

9. WshShell.Variable Question

10. Creating and environment variable and using it in the same cmd window

11. Environment variables in postscript templates

12. Getting Environment Variables

 

 
Powered by phpBB® Forum Software