Under WSH (Windows Script Host), you can access the COM object WScript.Shell for some useful functions. For example, the collection Environment provides the environment variables, so you can easily use either JScript or VBScript to access some particular information.
VBScript
The following is the VBScript that gets the variables from “Process”.
1 2 3 4 5 6 7 8 | Dim WSHShell C_LRF = Chr(13) & Chr(10) Set WSHShell = WScript.CreateObject("WScript.Shell") For Each strEnv In WshShell.Environment("PROCESS") s = s + strEnv + C_LRF Next WScript.Echo s |
Dim WSHShell C_LRF = Chr(13) & Chr(10) Set WSHShell = WScript.CreateObject("WScript.Shell") For Each strEnv In WshShell.Environment("PROCESS") s = s + strEnv + C_LRF Next WScript.Echo s
JScript
The following is the JScript that gets the variables from “Process”. You will need to use the trick Emulator to loop all variables.
1 2 3 4 5 6 7 8 9 10 | (function(){ var WSHShell = new ActiveXObject("WScript.Shell"); var Env = WSHShell.Environment("PROCESS"); var s = ""; var colVars = new Enumerator(Env); for(; !colVars.atEnd(); colVars.moveNext()) { s += colVars.item() + "\n"; } WScript.Echo(s); })(); |
(function(){ var WSHShell = new ActiveXObject("WScript.Shell"); var Env = WSHShell.Environment("PROCESS"); var s = ""; var colVars = new Enumerator(Env); for(; !colVars.atEnd(); colVars.moveNext()) { s += colVars.item() + "\n"; } WScript.Echo(s); })();
Read and Write
You can use method Item (default, so can be omitted) to set the particular variables:
1 2 3 | Set objShell = WScript.CreateObject("WScript.Shell") objShell.Environment("USER").Item("MyVar1") = "hello" objShell.Environment("USER").Item("MyVar2") = "world" |
Set objShell = WScript.CreateObject("WScript.Shell") objShell.Environment("USER").Item("MyVar1") = "hello" objShell.Environment("USER").Item("MyVar2") = "world"
And you can get the value if you know the name of the variable,
1 2 3 4 5 6 | 'Read Env Variables WScript.Echo objShell.Environment("USER").Item("MyVar1") WScript.Echo objShell.Environment("USER").Item("MyVar2") ' Retrieve the %COMPUTERNAME% system environment variable WScript.Echo objShell.Environment("PROCESS").Item("COMPUTERNAME") |
'Read Env Variables WScript.Echo objShell.Environment("USER").Item("MyVar1") WScript.Echo objShell.Environment("USER").Item("MyVar2") ' Retrieve the %COMPUTERNAME% system environment variable WScript.Echo objShell.Environment("PROCESS").Item("COMPUTERNAME")
The “.Item” is actually the default property and can be omitted.
Example Results
Process
ALLUSERSPROFILE=C:\ProgramData APPDATA=C:\Users\DrLai\AppData\Roaming asl.log=Destination=file CA65_INC=C:\Program Files (x86)\cc65\asminc CC65_INC=C:\Program Files (x86)\cc65\include CommonProgramFiles=C:\Program Files (x86)\Common Files CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files CommonProgramW6432=C:\Program Files\Common Files COMPUTERNAME=DOCTORLAI ComSpec=C:\Windows\system32\cmd.exe FP_NO_HOST_CHECK=NO HOMEDRIVE=C: HOMEPATH=\Users\DrLai IBREDISTDIR=C:\Users\Public\Documents\InterBase\redist\InterBaseXE3 LD65_CFG=C:\Program Files (x86)\cc65\cfg LD65_LIB=C:\Program Files (x86)\cc65\lib LD65_OBJ=C:\Program Files (x86)\cc65\obj LOCALAPPDATA=C:\Users\DrLai\AppData\Local LOGONSERVER=\\DOCTORLAI LUA_CPATH=C:\lua MOZ_PLUGIN_PATH=C:\Program Files (x86)\Foxit Software\Foxit Reader\plugins\ NUMBER_OF_PROCESSORS=8 NVIDIAWHITELISTED=0x01 OS=Windows_NT Path=C:\Program Files (x86)\nodejs;C:\Program Files\Java\jdk1.7.0_21\bin;C:\MinGW\bin;... PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC PROCESSOR_ARCHITECTURE=x86 PROCESSOR_ARCHITEW6432=AMD64 PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 42 Stepping 7, GenuineIntel PROCESSOR_LEVEL=6 PROCESSOR_REVISION=2a07 ProgramData=C:\ProgramData ProgramFiles=C:\Program Files (x86) ProgramFiles(x86)=C:\Program Files (x86) ProgramW6432=C:\Program Files PSModulePath=C:\Windows\system32\WindowsPowerShell\v1.0\Modules\ PUBLIC=C:\Users\Public SESSIONNAME=Console SHIM_MCCOMPAT=0x810000001 SystemDrive=C: SystemRoot=C:\Windows TEMP=E:\Temp TMP=E:\Temp USERDOMAIN=DoctorLai USERDOMAIN_ROAMINGPROFILE=DoctorLai USERNAME=DrLai USERPROFILE=C:\Users\DrLai VBOX_MSI_INSTALL_PATH=C:\Program Files\Oracle\VirtualBox\ VS100COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\ VS110COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\ VS120COMNTOOLS=F:\VS2013\Common7\Tools\ windir=C:\Windows WX=C:\wxWidgets-2.9.4 WXWIN=C:\wxWidgets-2.9.4
User
CA65_INC=C:\Program Files (x86)\cc65\asminc CC65_INC=C:\Program Files (x86)\cc65\include LD65_CFG=C:\Program Files (x86)\cc65\cfg LD65_LIB=C:\Program Files (x86)\cc65\lib LD65_OBJ=C:\Program Files (x86)\cc65\obj MOZ_PLUGIN_PATH=C:\Program Files (x86)\Foxit Software\Foxit Reader\plugins\ PATH=C:\Users\DrLai\AppData\Roaming\npm TEMP=E:\Temp TMP=E:\Temp
System
asl.log=Destination=file CA65_INC=C:\Program Files (x86)\cc65\asminc CC65_INC=C:\Program Files (x86)\cc65\include ComSpec=%SystemRoot%\system32\cmd.exe FP_NO_HOST_CHECK=NO IBREDISTDIR=C:\Users\Public\Documents\InterBase\redist\InterBaseXE3 LD65_CFG=C:\Program Files (x86)\cc65\cfg LD65_LIB=C:\Program Files (x86)\cc65\lib LD65_OBJ=C:\Program Files (x86)\cc65\obj LUA_CPATH=C:\lua NUMBER_OF_PROCESSORS=8 OS=Windows_NT Path=C:\Program Files (x86)\CollabNet;... PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC PROCESSOR_ARCHITECTURE=AMD64 PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 42 Stepping 7, GenuineIntel PROCESSOR_LEVEL=6 PROCESSOR_REVISION=2a07 PSModulePath=%SystemRoot%\system32\WindowsPowerShell\v1.0\Modules\ TEMP=E:\Temp TMP=E:\Temp USERNAME=SYSTEM VS100COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\ windir=%SystemRoot% WX=C:\wxWidgets-2.9.4 WXWIN=C:\wxWidgets-2.9.4 VS110COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\ VS120COMNTOOLS=F:\VS2013\Common7\Tools\ VBOX_MSI_INSTALL_PATH=C:\Program Files\Oracle\VirtualBox\
Volatile
LOGONSERVER=\\DOCTORLAI USERDOMAIN=DoctorLai USERNAME=DrLai USERPROFILE=C:\Users\DrLai HOMEPATH=\Users\DrLai HOMEDRIVE=C: APPDATA=C:\Users\DrLai\AppData\Roaming LOCALAPPDATA=C:\Users\DrLai\AppData\Local USERDOMAIN_ROAMINGPROFILE=DoctorLai
–EOF (The Ultimate Computing & Technology Blog) —
Last Post: Tutorial Example: How to Access Microsoft Excel (Chart) using the Windows Scripting Host (JScript)
Next Post: How to Change All Filenames to Lowercase Under BASH for Files under Directories and Sub-Directories