Jump to content

Expanding Environment Vars


Recommended Posts

So lets say you are reading an INI file. You read an item that is "%ComSpec%". The AutoIt option ExpandEnvStrings does not effect this since it's read from a file. I know you could use EnvGet to get the variable. But what about if from the INI you read "%.%temp%.%comspec%.%bob%.%.%". Does anyone have a UDF to expand this?

Thanks

Link to comment
Share on other sites

The first line is an example...

MsgBox(0, "", "%COMSPEC% = " & ParseEnvVar("%COMSPEC%"))

   Func ParseEnvVar($EnvVar)
       $var = StringMid($EnvVar, 2, StringLen($EnvVar) - 2)
       Return EnvGet($var)
   EndFunc
Edited by SlimShady
Link to comment
Share on other sites

Thanks Slim, but that really dosn't help too much. I figured it out this morning when i sobered up. This function will expand all the environment variables in a string.

Func ExpandEnvVars($Cooked)
    Local $Split, $i, $Expanded
    $Split = StringSplit($Cooked,"%")
    
    For $i = 1 To $Split[0]
        $Expanded = EnvGet($Split[$i])
        If $Expanded <> "" Then
            $Cooked = StringReplace($Cooked,"%" & $Split[$i] & "%", $Expanded)
        EndIf
    Next

    Return $cooked
EndFunc
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...