This is general purpose code that accepts a string as the name of an environment variable, and returns a string with the value of the variable.
If an invalid variable name is entered the following string is returned instead:
"Environment variable " + vEnvVariable + " is not defined"
Usage could include getting the USERNAME, SESAME_REPORT_PATH, USERDOMAIN, USERPROFILE, COMPUTERNAME, HOMEPATH, OS, OSVER, just to mention a few normal variables.
Function fnGetEnvVariable(vEnvVariable as String) as String
/* ==========================
Created by Bob Hansen, Sensible Solutions Inc., Salem NH rmhansense@sensiblesolutions.org
NOTE: This will not work in WIN9X environments.
It will work in XP/2K environments with the following qualification.
Sending SET plus a variable name will return ALL names that begin with the name asked for.
Example SET Temp may return many names that start with Temp.
Check your own operating system for more infor re the SET command.
In Microsoft systems type SET /? into the command prompt window or on the Start,Run utility
vAction has different values remarked out for Linux systems, provided by Mark Lasersohn.
// ======================================
*/
var vEnvFile as String //Temp file name to hold system variable info
var vOK as Int //For @SHELL
var vFileHandle1 as Int //Identifier for temp file to be opened
var vEnvValueString as String //File string containing system variable info
var vEnvValue as String //Value for the specified system variable
var vPos as Int //Position indicator in string
var vLen as Int //Length of a string
var vBatchFile as String //Name of temp batch file
var vAction as String //The String to retrieve the environment value
vEnvFile = "GetEnvValue.txt"
vEnvValueString = ""
vBatchFile = "GetEnvValue.bat"
vAction = "SET " + vEnvVariable +" >" + vEnvFile
//For LINUX, use one of the following lines for vAction instead of the line above.
//depending on which shell (sh, ksh, csh, bash, tcsh, ...), is running.
//vAction = "set | grep -i " + vEnvVariable + " > " + vEnvFile
//vAction = "printenv | grep -i " + vEnvVariable + " > " + vEnvFile
//If file exists for someone else then wait 5 seconds and try again.
While FileExists(vEnvFile) {
LOITER(5000)
FileDelete(vEnvFile)
}
//Start Function Execution =======================================
//Delete file if it exists
If FileExists(vBatchFile) THEN {
FileDelete(vBatchFile)
}
//Create Temp Batch File =======================
vFileHandle1 = fileOpen(vBatchFile)
//Write to Temp Batch File =======================
FileWriteLn(vFileHandle1,"@echo off")
FileWriteLn(vFileHandle1, vAction)
FileWriteLn(vFileHandle1, "IF errorlevel==1 goto NG")
FileWriteLn(vFileHandle1, "")
FileWriteLn(vFileHandle1, ":OK")
FileWriteLn(vFileHandle1, "goto End")
FileWriteLn(vFileHandle1, "")
FileWriteLn(vFileHandle1, ":NG")
FileWriteLn(vFileHandle1, "echo BAD-BAD-BAD > " + vEnvFile)
FileWriteLn(vFileHandle1, "")
FileWriteLn(vFileHandle1, ":End")
//Close Batch file
FileClose(vFileHandle1)
//Run temp batch file to redirect value to a temp file
vOK=@AsynchShell(vBatchFile)
//Delete File =======================
Loiter(500)
If FileExists(vBatchFile) THEN {
FileDelete(vBatchFile)
}
//Read the file line and delete the file
vFileHandle1 = FileOpen(vEnvFile)
FileSeek(vFileHandle1,0)
FileReadLn(vFileHandle1, vEnvValueString)
FileClose(vFileHandle1)
FileDelete(vEnvFile)
//Parse out the variable, from position of "=" to end of string
vLen = @Len(vEnvValueString)
vPos = @InStr(vEnvValueString,"=")
If vPos = 0 THEN {
vEnvValue = "Environment variable " + vEnvVariable + " is not defined"
} ELSE {
vEnvValue = @MID(vEnvValueString, vPos+1, vLen-vPos)
}
Return vEnvValue
End Function
//=============================================================
History: I tried to use @SHELL to redirect SET to a file. Worked OK but if input was bad then Sesame crashed.
So I decided to create a batch file to capture the ERROR code from the SET command.
That worked OK. But I did not want to distribute a batch file. So I am creating a temporary batch file, on the fly, passing in the environment variable asked for. And deleting the file when done with it.
Using @AsynchShell to run the batch file, then deleting the file after a short delay to allow it time to run. The results of SET are redirected to a temporary file that contains one line with the variable and its value. That file is also deleted when done with it.
Finally, the value is then parsed out and returned as a string value from the function.
If a bad variable name is requested, a string comes back saying that the variable name was invalid. The bad name is identified by looking for the absence of "=". The temp batch file actually writes "BAD-BAD-BAD" into the string when it receives an errorlevel code.
I have been unable to stop the "black box flash" from @AsynchShell, but that is a minor inconvenience right now.
Thanks to
albeamer for the idea and to
Mark Lasersohn for the Linux lines and the info re errorlevel codes. The original topic that provided me with the idea is
UserID from environment?
As noted in the code, this will not work on WIN9X systems, and Linux requires a different vAction line which is included, but remarked out.