Ok I know environment variables are not as popular as they once use to be, but they can be very powerful in setting up unique values on the machine for company settings. As you might have noticed in a few of the past postings I have been releasing sample code that uses the FSO (File System Object) of the Shell object.
This sample follows that same trend and uses the Shell object to manipulate environment variables that are maintained by the OS. Below are three, yes I said three different examples of using the Shell object. The first shows how to expand environment variables in a string. This can be great for building file paths or getting information about the OS or PC.
;; Shows how to use expanding environment strings
;; Usage: (ExpEnvStr "%TEMP%\\MYDATA")
;; Results of sample: "C:\\DOCUME~1\\Lee\\LOCALS~1\\Temp\\MYDATA"
(defun ExpEnvStr ( strVal / wshShell)
(vl-load-com)
(setq wshShell (vlax-create-object "WScript.Shell"))
(vlax-invoke-method wshShell 'ExpandEnvironmentStrings strVal)
)
;; Retreive the value of the environment variable
;; Usage: (GetEnvStr "SYSTEM" "USERID")
(defun GetEnvStr ( strVarType strVarName / wshShell envVars)
(vl-load-com)
(setq wshShell (vlax-create-object "WScript.Shell"))
(setq envVars (vlax-get-property wshShell 'Environment strVarType))
(vlax-get-property envVars 'Item strVarName)
)
;; Set the value to an environment variable
;; Usage: (SetEnvStr "SYSTEM" "USERID" "L123")
(defun SetEnvStr ( strVarType strVarName strVarVal / wshShell envVars)
(vl-load-com)
(setq wshShell (vlax-create-object "WScript.Shell"))
(setq envVars (vlax-get-property wshShell 'Environment strVarType))
(vlax-put-property envVars 'Item strVarName strVarVal)
)
Got an idea for some sample code? If you do, I would like to hear from you. Your idea might be selected and the results posted on the blog or website.
Sincerely,
Lee
Recent Comments