udfStrExpandEnvStrings
str udfStrExpandEnvStrings (str)
;------------------------------------------------------------------------------------------------------------------------------------------
#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'


; 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