+ Post New Thread
Results 1 to 3 of 3
Scripts Thread, Environment Variables Again in Coding and Web Development; I've seen a few threads on adding Environment Variables using VBScript but I can't seem to find information on retrieving ...
  1. #1

    Join Date
    Apr 2008
    Posts
    405
    Thank Post
    22
    Thanked 13 Times in 10 Posts
    Rep Power
    16

    Environment Variables Again

    I've seen a few threads on adding Environment Variables using VBScript but I can't seem to find information on retrieving a particular Variable and then changing the value.


    Any ideas?

  2. #2

    SYNACK's Avatar
    Join Date
    Oct 2007
    Posts
    11,186
    Thank Post
    875
    Thanked 2,717 Times in 2,302 Posts
    Blog Entries
    11
    Rep Power
    780
    This is a rather comprehensive list of methods to add, remove and change them using vbs

    Add, Remove, or Retrieve Environment Variables
    Code:
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    'Created by: Rob Olson - Dudeworks 
    'Created on: 10/17/2001 
    'Purpose: Get Environment Variables. 
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    wscript.echo "Working with the Environment: Provided by www.dudeworks.net"&vbcrlf&vbcrlf&strval 
    
    '// Create an instance of the wshShell object
    set WshShell = CreateObject("WScript.Shell")
    'Use the methods of the object
    wscript.echo "Environment.item: "& WshShell.Environment.item("WINDIR")
    wscript.echo "ExpandEnvironmentStrings: "& WshShell.ExpandEnvironmentStrings("%windir%")
    
    '// add and remove environment variables
    '// Specify the environment type ( System, User, Volatile, or Process )
    set oEnv=WshShell.Environment("System")
    
    wscript.echo "Adding ( TestVar=Windows Script Host ) to the System " _
    & "type environment"
    ' add a var
    oEnv("TestVar") = "Windows Script Host"
    
    wscript.echo "removing ( TestVar=Windows Script Host ) from the System " _
    & "type environment"
    ' remove a var
    oEnv.Remove "TestVar"
    
    
    '// List all vars in all environment types
    
    '//System Type
    set oEnv=WshShell.Environment("System")
    for each sitem in oEnv 
    strval=strval & sItem &vbcrlf 
    next
    wscript.echo "System Environment:"&vbcrlf&vbcrlf&strval 
    strval=""
    
    '//Process Type
    set oEnv=WshShell.Environment("Process")
    for each sitem in oEnv 
    strval=strval & sItem &vbcrlf 
    next
    wscript.echo "Process Environment:"&vbcrlf&vbcrlf&strval 
    strval=""
    
    '//User Type
    set oEnv=WshShell.Environment("User")
    for each sitem in oEnv 
    strval=strval & sItem &vbcrlf 
    next
    wscript.echo "User Environment:"&vbcrlf&vbcrlf&strval 
    strval=""
    
    '//Volatile Type
    set oEnv=WshShell.Environment("Volatile")
    for each sitem in oEnv 
    strval=strval & sItem &vbcrlf 
    next
    
    wscript.echo "Volatile Environment:"&vbcrlf&vbcrlf&strval 
    strval=""

  3. Thanks to SYNACK from:

    Reaper (19th February 2009)

  4. #3

    Join Date
    Apr 2008
    Posts
    405
    Thank Post
    22
    Thanked 13 Times in 10 Posts
    Rep Power
    16
    Thanks for your help with that, I managed to create a script to do what I needed doing. A snippet is below in case anyone wants to do a similar thing. Just to clarify, we use the Location variable to determine which printers we want that PC to be connected to. The connection is achieved via a script that looks at the Location variable.


    Code:
    set WshShell = CreateObject("WScript.Shell")
    
    set oEnv=WshShell.Environment("System")
    set oPro=WshShell.Environment("Process")
    
    dim cn, result
    
    cn = cstr(oPro("COMPUTERNAME"))
    result = left(cn,4)
    IF result = "IT-O" then
    	oEnv("LOCATION") = "IT"
    elseif result = "ART-" then
    	oEnv("LOCATION") = "ART"
    elseif result = "LRC" then
    	oEnv("LOCATION") = "LIBRARY"

SHARE:
+ Post New Thread

Similar Threads

  1. PHP variables
    By Hightower in forum Web Development
    Replies: 8
    Last Post: 2nd December 2008, 01:54 PM
  2. using variables in wget etc
    By RabbieBurns in forum *nix
    Replies: 6
    Last Post: 31st July 2008, 09:23 PM
  3. TEMP and TMP variables Deleted?
    By techyphil in forum Windows
    Replies: 5
    Last Post: 23rd May 2008, 03:09 PM
  4. Server Variables on CC3
    By gibbo_ap in forum Web Development
    Replies: 2
    Last Post: 24th April 2008, 02:48 PM
  5. Environment variables or other method
    By HodgeHi in forum Scripts
    Replies: 3
    Last Post: 21st June 2006, 09:05 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •