replace Environment variable within e.g. a path

Share your advanced PureBasic knowledge/code with the community.
Klonk
Enthusiast
Enthusiast
Posts: 173
Joined: Tue Jul 13, 2004 2:17 pm

replace Environment variable within e.g. a path

Post 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"
Bye Karl
horst
Enthusiast
Enthusiast
Posts: 197
Joined: Wed May 28, 2003 6:57 am
Location: Munich
Contact:

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

Post by horst »

Do you know the API function ExpandEnvironmentStrings_() ?
Horst.
Klonk
Enthusiast
Enthusiast
Posts: 173
Joined: Tue Jul 13, 2004 2:17 pm

Post by Klonk »

No.. Does this function do the same??? If yes :oops:
Bye Karl
horst
Enthusiast
Enthusiast
Posts: 197
Joined: Wed May 28, 2003 6:57 am
Location: Munich
Contact:

Post 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:
Horst.
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1285
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post 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
Image Image
Fred
Administrator
Administrator
Posts: 18237
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

This routine can be useful on linux or OS X as it will need only minor tweak (if no tweak at all).
Rinzwind
Enthusiast
Enthusiast
Posts: 690
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

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

Post 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
Post Reply