Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

function: Expand paths with environement variables


  • Please log in to reply
12 replies to this topic
majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
#NoEnv 
 ppath = blabla bla asd `%TEMP`%....`%SYSTEMROOT`%.. 
 MsgBox % ExpandEnvVars(ppath) 
return 

ExpandEnvVars(ppath) 
{
	VarSetCapacity(dest, 2000) 
	DllCall("ExpandEnvironmentStrings", "str", ppath, "str", dest, int, 1999, "Cdecl int") 
	return dest
}

-------------------------------------------------------
another example for those that still ask why this:

Open cmd.exe
type:
  set w=c:\windows
  set s= \system32
  set n= \notepad.exe

  %w%%s%%n%

This function enables you to expand env variables that you receive in the string, by reading some config, ini, or something.....
Posted Image

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
I wondered why you do this by hand, but indeed, it make sense with the #NoEnv directive.
The alternative is to import all environment variables (or omit #NoEnv) and use Transform Deref, but it can be unwanted to create so much variables.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
even withouth NoEnv, I recieve env vars in the STRING, and I didn't find the way to expand this string. Take this for example. For instance, this is in ini file:

[Menu]
menu1=Notepad
cmnd1=%w%%s%%n%If I use this code to obtain this line:
IniRead myCmnd, iniFile, section, .....I will get myCmnd containing environement varialbes that point to executable. The only way to execute is then
myCmnd := ExpandEnvVars(myCmnd)
IfExist myCmnd
Run myCmndWhen I tried to expand differently I got errors. I tried:

;---- #NoEnv
ppath = `%TEMP%`

full_path := %ppath% (will not do)
full_path = % %ppath% (the same as above)

So, if string contains environement variables, there is no way in witch you can expand them except manualy, since existing functions with files are NOT aware of env vars.
Posted Image

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
Here's a shorter version:
#NoEnv

string = something something `%Temp`% something..`n`%SystemRoot`%

MsgBox, % expand(string)



expand(str) {

	Loop, Parse, str, `%

	If !Mod(A_Index, 2) {

		EnvGet, v, %A_LoopField%

		StringReplace, str, str, `%%A_LoopField%`%, %v%

	} Return, str

}

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
:lol:

Cool... thx for the note.
PhilLo made this obsolete with Transform
Posted Image

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
Oh I never saw his post. I never knew about Transform, Deref either, cool.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


Micha
  • Members
  • 539 posts
  • Last active: Dec 31 2011 01:43 PM
  • Joined: 15 Nov 2005
Hi,
or if you want to use a dllcall

#NoEnv 
 ppath = blabla bla asd `%TEMP`%....`%SYSTEMROOT`%..
 VarSetCapacity(Dest, 2000)
 nRC := DllCall("ExpandEnvironmentStrings", "str", ppath, "str", Dest, int, 1999, "Cdecl int") 
 MsgBox pPath:%ppath% - expanded:%Dest% - nRC:%nRC% 
return
Ciao
Micha

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
@Micha: Good idea because it is usable even with #NoEnv.
And to expand AutoHotkey's variables, we still have the built-in solution.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
Well, I drawback that it is obsolete since I didn't know Transform doesn't work witht #NoEnv.

So, we will use either Michas dllcall or titans reduced script. I guess DllCall then, since it must be faster.
Posted Image

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006

Oh I never saw his post. I never knew about Transform, Deref either, cool.


Well, that is a problem. If experienced users as you can not find something as .... hm.... novice... as this, than I count this as a documentation problem.
Posted Image

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
The only problem with the documentation is also its main advantage: it is large! Problem because few people really read it from first to last page. Advantage because few freewares have so much documentation.
Chris is struggling to keep a balance between enough information (so anything useful is there) and too much (where important information is buried under details).
I grant you finding this command isn't obvious (I don't recall how I discovered it), but which way would you use to make it easier to find? It doesn't look like a primordial command, used in most scripts...
Chris already tried to make obvious what commands are to look at in priority.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
I updated first post to reflect what we learnd so far.

2PhilLo

It doesn't look like a primordial command, used in most scripts...

Command itself isn't obvious nor fundamental but what it does with evironement variables is.

The only problem with the documentation is also its main advantage

I don't agree. But we will talk soon about this.
Posted Image

wOxxOm
  • Members
  • 371 posts
  • Last active: Feb 20 2015 12:10 PM
  • Joined: 09 Feb 2006
WinApi sdk says that ExpandEnvironmentStrings is stdcall (WINAPI), then why use "Cdecl int" ?

Tip: To replace folder names in a fully-qualified path with their associated environment-variable strings, use the PathUnExpandEnvStrings function

BOOL PathUnExpandEnvStrings(LPCTSTR pszPath, LPTSTR pszBuf, UINT hBuf);