Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

UDF: _ExpandEnvStrings()


  • Please log in to reply
5 replies to this topic

#1 blindwig

blindwig

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 772 posts

Posted 09 June 2005 - 02:20 AM

I ran into some quirks while trying to control environmental variable expansion using the ExpandEnvStrings option, so I wrote a function to parse a string and return an expanded version of that string:
CODE

Func _ExpandEnvStrings($sInput)
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 is to be returned literally
$sOut = $sOut & '%' & $aPart[$i]
EndIf
$i = $i + 1
WEnd
Return $sOut
EndFunc

Example:
CODE

MsgBox(0, '', _ExpandEnvStrings('the file "%comspec%" is your command interpreter'), 5)








#2 FuryCell

FuryCell

    A cornered fox is more dangerous than a jackal!

  • Active Members
  • PipPipPipPipPipPip
  • 2,437 posts

Posted 12 June 2005 - 12:54 PM

Awesome function! :(
Just tried on this string.
"Path=%PATH% %%"

It even knew to take the two %'s literally.
Better than Opt("ExpandEnvStrings",1) becuase as you said it's not quirky and it does not affect all strings , only the ones you want.
Good Job! :(

P.S. I might make an _ExpandVariables func like this if you woundn't mind me using your idea.

Edited by SolidSnake, 12 June 2005 - 01:01 PM.

HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.

#3 blindwig

blindwig

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 772 posts

Posted 13 June 2005 - 05:45 AM

Awesome function!  :( 
Just tried on this string.

"Path=%PATH% %%"

It even knew to take the two %'s literally.
Better than Opt("ExpandEnvStrings",1) becuase as you said it's not quirky and it does not affect all strings , only the ones you want.
Good Job! :(

P.S. I might make an _ExpandVariables func like this if you woundn't mind me using your idea.

<{POST_SNAPBACK}>

Yup, I tested it and tried to make it as close to "normal" (ie, as the command intrepreter would handle it) as I could. It successfully handles these tricky ones:
'%%path%%'
'%path%path%'

If you want to make a generic version (one that can use any character to "wrap" a variable), Change the header:
Func _ExpandEnvStrings($sInput, $Delim='%')
And then replace any '%' in the function with $Delim.
Replace the EnvGet with your own function, or I'd recommend using my table UDF to store your own variables.

#4 blindwig

blindwig

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 772 posts

Posted 04 July 2005 - 07:06 PM

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


#5 MAli

MAli

    Seeker

  • New Members
  • 1 posts

Posted 19 March 2013 - 09:20 AM

Here is another example using RegEx:

AutoIt         
Func _ExpandEnvStrings($sExpr)     Local Const $sPattern = "\%[\d\w()_]+\%"     Local $a     Local $sEnv     Local $p1, $p2     Local $sMatch     Local $sMatchStripPct     Local $sResult     $a = StringRegExp ($sExpr, $sPattern, 3)     $sResult = $sExpr     If IsArray($a) Then         For $sMatch In $a             $p1 = StringInStr($sMatch, "%", 0, 1)             $p2 = StringInStr($sMatch, "%", 0, 2)             $sMatchStripPct = StringMid($sMatch, $p1+1, $p2-$p1-1)             $sEnv = EnvGet($sMatchStripPct)             If $sEnv = "" Then $sEnv = $sMatch             $sResult = StringReplace($sResult, $sMatch, $sEnv, 1)         Next     EndIf     Return $sResult EndFunc

Edited by MAli, 19 March 2013 - 09:49 AM.


#6 Melba23

Melba23

    I'm old - what's your excuse?

  • Moderators
  • 20,950 posts

Posted 19 March 2013 - 01:06 PM

MAli,

Oh come on! The post above yours dates from nearly 8 years ago. Please pay attention when you post - we do not approve of resurrecting threds this old as the language has changed so much since then. :naughty:

M23

P.S. Welcome to the AutoIt forum anyway - just be more careful in future. ;)

Edited by Melba23, 19 March 2013 - 01:06 PM.
Fixed BB tags

StringSize - Automatically size controls to fit text                                                               ExtMsgBox - A user customisable replacement for MsgBox
Toast - Small GUIs which pop out of the Systray                                                                Marquee - Scrolling tickertape GUIs
Scrollbars - Automatically sized scrollbars with a single command                                   GUIFrame - Subdivide GUIs into many adjustable frames
GUIExtender - Extend and retract multiple sections within a GUI                                      NoFocusLines - Remove the dotted focus lines from buttons, sliders, radios and checkboxes
ChooseFileFolder - Single and multiple selections from specified path tree structure      Notify - Small notifications on the edge of the display
Date_Time_Convert - Easily convert date/time formats, including the language               GUIListViewEx - Insert, delete, move, drag, sort and edit ListView items




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users