CFLib.org – Common Function Library Project

GetEnv([varName])

Last updated March 11, 2003

author

Ben Forta

Version: 1 | Requires: CF6 | Library: UtilityLib

Description:
Returns environment information. (Windows only!)

Return Values:
Returns either a structure or a string.

Example:

<!---
<cfoutput>
<cfdump var="#getenv()#">
Path: #getenv("path")#<br>
</cfoutput>
--->

Parameters:

Name Description Required
varName Environment variable to return. No

Full UDF Source:

<!---
 Returns environment information. (Windows only!)
 
 @param varName 	 Environment variable to return. (Optional)
 @return Returns either a structure or a string. 
 @author Ben Forta (ben@forta.com) 
 @version 1, March 11, 2003 
--->
<cffunction name="GetEnv" output="false" returnType="any">
	<cfargument name="varname" type="string" required="no">
	
	<!--- Define local variables --->
	<cfset var env=structNew()>
	<cfset var data="">
	<cfset var ename="">
	<cfset var evalue="">
	<cfset var i = 1>
	
	<!--- Get enviroment and save to variable --->
	<cfsavecontent variable="data">
		<cfexecute name="cmd /c set" timeout="1" />
	</cfsavecontent>

	<!--- Need to convert to structure, so loop through --->
	<cfloop index="i" list="#trim(data)#" delimiters="#chr(10)##chr(13)#">
		<!--- For each line, get name and value --->
		<cfset ename=trim(listfirst(i, "="))>
		<cfset evalue=trim(listrest(i, "="))>
		<!--- And add to structure --->
		<cfset env[ename] = evalue>
	</CFLOOP>

	<!--- And finally, build return value --->
	<cfif not isDefined("varname")>
		<!--- If no name, return list --->
		<cfreturn env>
	<cfelseif structKeyExists(env, varname)>
		<!--- If name provided, and present, get value --->
		<cfreturn env[varname]>
	</cfif>

</cffunction>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson