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

System UDFs

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

Environment Variable UDFs

 Keywords: Expand Environment Variable 

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfStrExpandEnvStrings (strExpSz)
If strExpSz == "" Then Return ""
intPos = 1
intLen = 0
While @TRUE
   intPos = StrIndexWild (strExpSz, "%%*%%", intPos + intLen)
   If !intPos Then Break
   strSearch = StrSubWild (strExpSz, "%%*%%", intPos)
   intLen = StrLen (strSearch)
   If intLen > 2
      strItemList = @LF : EnvItemize () : @LF
      strSearch = @LF : StrLower (StrReplace (strSearch, "%%", "")) : "=*" : @LF
      intPosItem = StrIndexWild (StrLower (strItemList), strSearch, 1)
      If intPosItem
         strItem = StrSub (strItemList, intPosItem + 1, StrIndex (strItemList, @LF, intPosItem + 1, @FWDSCAN) - intPosItem - 1)
         strItem = StrSub (strItem, StrIndex (strItem, "=", 1, @FWDSCAN) + 1, -1)
         If strItem != "" Then strExpSz = StrSub (strExpSz, 1, intPos - 1) : strItem : StrSub (strExpSz, intPos + intLen, -1)
         intLen = StrLen (strItem)
      EndIf
   Else
      strExpSz = StrSub (strExpSz, 1, intPos - 1) : "%%" : StrSub (strExpSz, intPos + 2, -1)
      intLen = 1
   EndIf
EndWhile
Return strExpSz
;..........................................................................................................................................
; This UDF "udfStrExpandEnvStrings" works on a given string (datatype ExpandSz),
; which contains one or more environment variable names enclosed in percent signs (e.g. [pct]path[pct]).
; If there exists a defined variable with such a name in the current environment (search is case insensitive),
; then this function replaces the name placeholder token with the value of the environment variable.
; This function bypasses the 'maximum value length is 1280 bytes' restriction of WB native function Environment()
; by utilizing the WB native function EnvItemize (), which returns the environment variable in its entire length.
;..........................................................................................................................................
; Detlev Dalitz.20030911.20090423.20090529.20090530.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.
strMsgTitle = "Demo: udfStrExpandEnvStrings (strString)"


strExpSz = ""
strExpSz = udfStrExpandEnvStrings (strExpSz)
Message (strMsgTitle:" - 1", strExpSz)
; ''

strExpSz = '%%'
strExpSz = udfStrExpandEnvStrings (strExpSz)
Message (strMsgTitle:" - 2", strExpSz)
; '%'

strExpSz = '%%%%'
strExpSz = udfStrExpandEnvStrings (strExpSz)
Message (strMsgTitle:" - 3", strExpSz)
; '%'

strExpSz = '%%%%%%'
strExpSz = udfStrExpandEnvStrings (strExpSz)
Message (strMsgTitle:" - 4", strExpSz)
; '%%'

strExpSz = '%%%%%%%%'
strExpSz = udfStrExpandEnvStrings (strExpSz)
Message (strMsgTitle:" - 5", strExpSz)
; '%%'

strExpSz = 'My %%%%UserName%%%% is "%%USERNAME%%" and my %%%%AppData%%%% folder is "%%ApPdAtA%%".'
strExpSz = udfStrExpandEnvStrings (strExpSz)
Message (strMsgTitle:" - 6", strExpSz)
; 'My %UserName% is "DD" and my %AppData% folder is "X:\DD\ApplicationData".'



strExpSz = "%%SystemRoot%%\System32;%%SystemRoot%%;%%SystemRoot%%\System32\Wbem"
strExpSz = udfStrExpandEnvStrings (strExpSz)
Message (strMsgTitle:" - 7", strExpSz)
; 'C:\WINDOWS\System32;C:\WINDOWS;C:\WINDOWS\System32\Wbem'Exit


; Set environment variable with length of 1280 byte, i. e. max. size Environment() can handle.
blnResult = EnvironSet ("Z", StrFill ("Z", 1280))

strEnvVar = "Z"
strResult = Environment (strEnvVar)
intLen = StrLen (strResult)
strMsgText = "Environment variable = " : strEnvVar : @LF : "Content = " : strResult : @LF : "Length = " : intLen
Message (strMsgTitle:" - 8", strMsgText)

; Set environment variable with length of one byte more than Environment() can handle.
blnResult = EnvironSet ("Z", StrFill ("Z", 1280 + 1))

strEnvVar = "Z"
strResult = Environment (strEnvVar)
intLen = StrLen (strResult)
strMsgText = "Environment variable = " : strEnvVar : @LF : "Content = " : strResult : @LF : "Length = " : intLen
Message (strMsgTitle:" - 9", strMsgText)

strEnvVar = "%%Z%%" ; Datatype ExpandSz.
strResult = udfStrExpandEnvStrings (strEnvVar)
intLen = StrLen (strResult)
strMsgText = "ExpandSz variable = " : strEnvVar : @LF : "Content = " : strResult : @LF : "Length = " : intLen
Message (strMsgTitle:" - 10", strMsgText)


; Set environment variable with a string value containing equal signs.
blnResult = EnvironSet ("Z", "A=B==C===D")

strEnvVar = "Z"
strResult = Environment (strEnvVar)
intLen = StrLen (strResult)
strMsgText = "Environment variable = " : strEnvVar : @LF : "Content = " : strResult : @LF : "Length = " : intLen
Message (strMsgTitle:" - 11", strMsgText)

strEnvVar = "%%Z%%" ; Datatype ExpandSz.
strResult = udfStrExpandEnvStrings (strEnvVar)
intLen = StrLen (strResult)
strMsgText = "ExpandSz variable = " : strEnvVar : @LF : "Content = " : strResult : @LF : "Length = " : intLen
Message (strMsgTitle:" - 12", strMsgText)

Exit



--------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfExpandEnvironmentStrings (strExpSz)
intBBSize = DllCall ("KERNEL32.DLL", long:"ExpandEnvironmentStringsA", lpstr:strExpSz, long:0, long:0)
hdlBB = BinaryAlloc (intBBSize)
BinaryEodSet (hdlBB, intBBSize)
intResult = DllCall ("KERNEL32.DLL", long:"ExpandEnvironmentStringsA", lpstr:strExpSz, lpbinary:hdlBB, long:intBBSize)
strExpSz = BinaryPeekStr (hdlBB, 0, intResult)
hdlBB = BinaryFree (hdlBB)
Return strExpSz
; Detlev Dalitz.20090529.
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.
strMsgTitle = "Demo: udfExpandEnvironmentStrings (strExpSz)"

:Test1
; Set environment variable with length of one byte more than Environment() can handle.
blnResult = EnvironSet ("Z", StrFill ("Z", 1280 + 1))
strExpSz = "%%Z%%"
strResult = udfExpandEnvironmentStrings (strExpSz)

intLen = StrLen (strResult)
strMsgText = "ExpandSz variable = " : strExpSz : @LF : "Content = " : strResult : @LF : "Length = " : intLen
Message (strMsgTitle, strMsgText)


:Test2
blnResult = EnvironSet ("Z", "A=B==C===D")
strExpSz = "%%Z%%"
strResult = udfExpandEnvironmentStrings (strExpSz)
; strResult ==> 'A=B==C===D'

intLen = StrLen (strResult)
strMsgText = "ExpandSz variable = " : strExpSz : @LF : "Content = " : strResult : @LF : "Length = " : intLen
Message (strMsgTitle, strMsgText)


:Test3
strExpSz = 'My %%%%UserName%%%% is "%%USERNAME%%" and my %%%%AppData%%%% folder is "%%ApPdAtA%%".'
strResult = udfExpandEnvironmentStrings (strExpSz)
; strResult ==> 'My %DD% is "DD" and my %X:\DD\ApplicationData% folder is "X:\DD\ApplicationData".'

strMsgText = StrCat (strExpSz, @LF, @LF, strResult)
Message (strMsgTitle, strMsgText)


:Test4
; See result of Test3, did we expected this result?
; If we want to save our percent signs, then we have to do do some pre/postprocessing.
strExpSz = 'My %%%%UserName%%%% is "%%USERNAME%%" and my %%%%AppData%%%% folder is "%%ApPdAtA%%".'
strTemp1 = StrReplace (strExpSz, "%%%%", "$$")
strTemp2 = udfExpandEnvironmentStrings (strTemp1)
strResult = StrReplace (strTemp2, "$$", "%%")
; strResult ==> 'My %UserName% is "DD" and my %AppData% folder is "X:\DD\ApplicationData".'

strMsgText = StrCat (strExpSz, @LF, @LF, strTemp1, @LF, @LF, strTemp2, @LF, @LF, strResult)
Message (strMsgTitle, strMsgText)

Exit



--------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfExpandEnvironmentStrings (strExpSz)
objWshShell = CreateObject ("WScript.Shell")
strExpSz = objWshShell.ExpandEnvironmentStrings(strExpSz)
objWshShell = 0
Return strExpSz
; Detlev Dalitz.20090529.
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

strMsgTitle = "Demo: udfExpandEnvironmentStrings (strExpSz) using WScript.Shell object."

strExpSz = 'My %%%%UserName%%%% is "%%USERNAME%%" and my %%%%AppData%%%% folder is "%%ApPdAtA%%".'
strResult = StrReplace (udfExpandEnvironmentStrings (StrReplace (strExpSz, "%%%%", "$$")), "$$", "%%")
; strResult ==> 'My %UserName% is "DD" and my %AppData% folder is "X:\DD\ApplicationData".'

strMsgText = StrCat (strExpSz, @LF, @LF, strResult)
Message (strMsgTitle, strMsgText)

Exit

Article ID:   W18399
Filename:   Environment Variable UDFs.txt
File Created: 2009:11:16:10:15:58
Last Updated: 2009:11:16:10:15:58