Page 1 of 1

replace Environment variable within e.g. a path

Posted: Wed Apr 05, 2006 8:22 am
by Klonk
Code updated for 5.20+

Hi I used this in a project, maybe someone finds it useful too:

Code: Select all

;{ ==========================================================================
;|  ReplaceEnvVar(text$)
;|    Returns the string text$ where all strings pointing to environment
;|    variables, e.g.(%windir%) are replaced by their content
;} ==========================================================================
Procedure.s ReplaceEnvVar(text$)
  ;*****Checks text$ if it contains %sometext% and replaces it with the
  ;     content of the environment variable sometext
  count.b = CountString(text$,"%")
  result$ = text$
  If count > 0  
    For i.b = 1 To count ;go through each part of the string surrounded by "%"
      If FindString(text$,"%",1)<>0
        part$ = Left(text$,FindString(text$,"%",1)-1)
      EndIf
      text$ = Right(text$,Len(text$)-Len(part$)-1)
      If (i<>1) And (part$<>"") And (GetEnvironmentVariable(part$)<>"")
        ;replace found stuff by content of environment variable
        result$ = ReplaceString(result$,"%"+part$+"%",GetEnvironmentVariable(part$))
      EndIf
    Next
  EndIf
  ProcedureReturn result$
EndProcedure
Main use is for paths, but could be used for other strings too. It replaces found links to environment variables e.g. %windir% with their content. Example:

Code: Select all

app$ = ReplaceEnvVar("%windir%\notepad.exe")
   ; app$ will be e.g. "c:\windows\notepad.exe"

Re: replace Environment variable within e.g. a path

Posted: Thu Apr 06, 2006 6:00 pm
by horst
Do you know the API function ExpandEnvironmentStrings_() ?

Posted: Sat Apr 08, 2006 7:19 am
by Klonk
No.. Does this function do the same??? If yes :oops:

Posted: Sat Apr 08, 2006 9:02 am
by horst
No reason to :oops:
I also had a hand made function for my PopSel program, until I discovered ExpandEnvironmentStrings_(). I think I used StringField()

In any case it's fun to produce solutions.. :wink:

Posted: Sat Apr 08, 2006 3:31 pm
by Paul
For those unfamiliar with API calls, this is what it would look like...

Code: Select all

buffer.s=Space(256)
ExpandEnvironmentStrings_("%windir%\notepad.exe",@buffer,Len(buffer))

Debug buffer

Posted: Sat Apr 08, 2006 4:37 pm
by Fred
This routine can be useful on linux or OS X as it will need only minor tweak (if no tweak at all).

Re: replace Environment variable within e.g. a path

Posted: Sat Sep 14, 2019 4:06 am
by Rinzwind
Another cross platform one (I know oooold topic)

Code: Select all

Procedure.s ExpandVars(text.s)
  Protected n.s, v.s, cv.s
  
  CompilerIf #PB_Compiler_OS <> #PB_OS_Windows
    cv = "$"
  CompilerElse
    cv = "%"
  CompilerEndIf  
  If ExamineEnvironmentVariables()
    While FindString(text, cv) > 0 And NextEnvironmentVariable() 
      n = EnvironmentVariableName()
      v = EnvironmentVariableValue()
      CompilerIf #PB_Compiler_OS <> #PB_OS_Windows
        If FindString(n, " ") = 0
          text = ReplaceString(text, cv + n, v)
        EndIf
        text = ReplaceString(text, "${" + n + "}", v)
      CompilerElse
        text = ReplaceString(text, cv + n + cv, v, #PB_String_NoCase)
      CompilerEndIf
    Wend
  EndIf
  CompilerIf #PB_Compiler_OS <> #PB_OS_Windows
    text = ReplaceString(text, "~/", GetHomeDirectory())  
  CompilerEndIf
  ProcedureReturn text
EndProcedure