OK, I just added another feature to this routine. Now, in addition to looking for variables like %SystemDir%, it also looks for passes parameters, such as %1 %2 %3 etc. It takes an optional array of parameters (this array is assumed to be 1-base, like the $CmdLine built-in) and replaces %1 with element 1 of the array, %2 with the element 2, etc.
I am now working on wrapping it to find default commands for a particular filetype (so that I don't have to do a ShellExecute, I can actually look at the shell command and execute it myself)
Here's the code:
CODE
;Expands environment variables in a string, such as %COMSPEC% etc
;Also looks for %1 %2 %3 etc and substitutes from $aParameter, which is a 1-based 1-dimensional array
Func _ExpandEnvStrings($sInput, $aParameter=0)
Dim $aPart = StringSplit($sInput,'%')
If @error Then
SetError(1)
Return $sInput
EndIf
Dim $sOut = $aPart[1], $i = 2, $env = ''
;loop through the parts
While $i <= $aPart[0]
$env = EnvGet($aPart[$i])
If $env <> '' Then ;this part is an expandable environment variable
$sOut = $sOut & $env
$i = $i + 1
If $i <= $aPart[0] Then $sOut = $sOut & $aPart[$i]
ElseIf $aPart[$i] = '' Then ;a double-percent is used to force a single percent
$sOut = $sOut & '%'
$i = $i + 1
If $i <= $aPart[0] Then $sOut = $sOut & $aPart[$i]
Else ;this part may be literal, or may request a parameter
;first check for a parameter request
if Not UBound($aParameter,0) = 1 Then
If IsString($aParameter) Then
$aParameter = _ArrayCreate(1, $aParameter)
Else
Local $aParameter[1]
EndIf
EndIf
Local $j = 1, $num
While $j <= StringLen($aPart[$i]) And StringIsDigit(stringmid($aPart[$i], $j, 1))
$j = $j + 1
WEnd
$Num = StringLeft($aPart[$i], $j - 1)
If StringIsInt($num) Then ;parameter requested
If Int($num)<=$aParameter[0] Then ;allowed
$sOut = $sOut & $aParameter[int($num)] & StringTrimLeft($aPart[$i], $j - 1)
Else ;denied
$sOut = $sOut & StringTrimLeft($aPart[$i], $j - 1)
EndIf
Else ;return literal
$sOut = $sOut & '%' & $aPart[$i]
EndIf
EndIf
$i = $i + 1
WEnd
Return $sOut
EndFunc