WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Samples from Users
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Getting the Environment Path

 Keywords: Environment Path

As you've discovered, retrieving the path data you need can be a little tricky. The 1280 character limit for the Environment function is a pain

NOTE:

  1. The Path returned is a combination of the System path and the User path (i.e. same as entering Path at Cmd prompt)
  2. When executed in an uncompiled script, the WinBatch folders are part of the temporary environment and so are returned in the Path variable. When the same script is run compiled, the WinBatch folders are not part of the path.
If you read the System or User path from the registry, any environment variables used within the path will NOT be expanded.

Here are a set of UDFs that should give you everything you need to overcome these issues. Note that the first UDF does the same thing as your routine, but more efficiently.

; ----------------------------------------------------------------------------------------------
; Returns a combined environment variable (System + User)
; This UDF will return the same data as the Environment function, but without a 1280 char limit
; In the case of the Path variable, this UDF (and the Environment function) will return:
;    - uncompiled script: WinBatch paths + System Path + User Path
;    - compiled script: System Path + User Path
; Les Ferch, June 2009

#DefineFunction GetEnv(Var)
aEnv = Arrayize(EnvItemize(),@LF)
Last = ArrInfo(aEnv,1) - 1
For i = 0 To Last
   x = StrIndex(aEnv[i],"=",2,@FWDSCAN)
   VarName = StrSub(aEnv[i],1,x - 1)
   If StrLower(VarName)!=StrLower(Var) Then Continue
   VarData = StrSub(aEnv[i],x + 1,-1)
   Return VarData
Next
#EndFunction


; ----------------------------------------------------------------------------------------------
; Returns a System environmemt variable
; RegQueryEx is used to allow registry entry to be REG_SZ or REG_EXPAND_SZ
; If we only read the registry value, we'll get values back that contain other variables,
; such as %SYSTEMROOT% so we call ExpandEnvVars to expand those variables
; Les Ferch, May 2009

#DefineFunction GetSystemEnv(Var)
VarData = ""
KeyVal = "System\CurrentControlSet\Control\Session Manager\Environment[":Var:"]"
If RegExistValue(@REGMACHINE,KeyVal)
   Type = RegEntryType(@REGMACHINE,KeyVal)
   VarData = RegQueryEx(@REGMACHINE,KeyVal,@TAB,Type)
   VarData = ExpandEnvVars(VarData)
EndIf
Return VarData
#EndFunction


; ----------------------------------------------------------------------------------------------
; Returns a User environmemt variable
; RegQueryEx is used to allow registry entry to be REG_SZ or REG_EXPAND_SZ
; If we only read the registry value, we'll get values back that contain other variables,
; such as %SYSTEMROOT% so we call ExpandEnvVars to expand those variables
; Les Ferch, May 2009

#DefineFunction GetUserEnv(Var)
VarData = ""
KeyVal = "Environment[":Var:"]"
If RegExistValue(@REGCURRENT,KeyVal)
   Type = RegEntryType(@REGCURRENT,KeyVal)
   VarData = RegQueryEx(@REGCURRENT,KeyVal,@TAB,Type)
   VarData = ExpandEnvVars(VarData)
EndIf
Return VarData
#EndFunction


; ----------------------------------------------------------------------------------------------
; Replaces environment variable names (indicated by percent signs) in strings with
; the actual value for that environment variable
; Les Ferch, June 2009

#DefineFunction ExpandEnvVars(Str)
aEnv = Arrayize(EnvItemize(),@LF)
Last = ArrInfo(aEnv,1) - 1
For i = 0 To Last
   x = StrIndex(aEnv[i],"=",2,@FWDSCAN)
   VarName = StrSub(aEnv[i],1,x - 1)
   VarData = StrSub(aEnv[i],x + 1,-1)
   Str = StrReplaceNC(Str,"%%":VarName:"%%",VarData)
Next
Return Str
#EndFunction


; ----------------------------------------------------------------------------------------------
; Case insensitive StrReplace

#DefineFunction StrReplaceNC(Str,Old,New)
Len = StrCharCount(Str)
OldLen = StrCharCount(Old)
NewLen = StrCharCount(New)
BBSize = Len
If NewLen>OldLen
   Count = 0
   i = 1
   While @TRUE
      i = StrIndexNC(Str,Old,i,@FWDSCAN)
      If i==0 Then Break
      Count = Count + 1
      i = i + OldLen
   EndWhile
   BBSize = BBSize + (Count * (NewLen - OldLen))
EndIf
BB = BinaryAlloc(BBSize)
BinaryPokeStr(BB,0,Str)
BinaryReplace(BB,Old,New,@FALSE)
NewStr = BinaryPeekStr(BB,0,BinaryEodGet(BB))
BinaryFree(BB)
Return(NewStr)
#EndFunction


; Test
Message("Combined",Environment("Path"))
Message("Combined",GetEnv("Path"))
Message("System",GetSystemEnv("Path"))
Message("User",GetUserEnv("Path"))
Message("System",GetSystemEnv("Temp"))
Message("User",GetUserEnv("Temp"))

; Tricky ExpandEnvVars Test
Message("",ExpandEnvVars("%%UserDomain%%%%UserName%%"))
Message("",ExpandEnvVars("%%UserDomain%%\%%UserName%%"))


Article ID:   W18247
Filename:   Getting the Environment Path .txt
File Created: 2009:06:12:13:15:10
Last Updated: 2009:06:12:13:15:10