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

Registry UDFs

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

Expand Environment Strings

 Keywords:  Environment Envitemize udfStrExpandEnvStrings ExpandSz

;------------------------------------------------------------------------------------------------------------------------------------------
#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.

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

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

strExpSz = ""
strExpSz = udfStrExpandEnvStrings (strExpSz)
Message ("", strExpSz)
; ''

Exit

Article ID:   W18385
Filename:   Expand Environment Strings.txt
File Created: 2009:06:01:09:25:54
Last Updated: 2009:06:01:09:25:54