Using environment variables

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Haswell
Posts: 90
Joined: 21 Feb 2016, 17:11

Using environment variables

26 Oct 2019, 03:00

I need to open a path specified with Windows environment variables listed here:
https://www.rapidee.com/en/environment-variables

This code gives me an error:

Code: Select all

path := "C:\Users\%username%\AppData\Roaming\Code\User"
Run, "%path%"
I know about A_username but the path is specified by a user who doesn't know about AHK-variables.
I also don't want to convert "username" to "A_username" because it's a tricky way.
User also might use any variable which is not presented in AHK.
Would you kindly help me to fix it?
gregster
Posts: 9034
Joined: 30 Sep 2013, 06:48

Re: Using environment variables

26 Oct 2019, 03:27

You can simply fix this error by using valid expression syntax (and not using #NoEnv):

Code: Select all

path := "C:\Users\" username "\AppData\Roaming\Code\User"
Run, %path%
Like this, you are using the username Env variable, but you are overwriting AHK's copy of the Path variable... but if you don't need it, that's ok.
Edit: there is also Env appdata available, see: msgbox % appdata

But tbh, I am not sure, if I understand what you specifically intend here. Probably you should read this: https://www.autohotkey.com/docs/Concepts.htm#environment-variables
With #NoEnv you could do this:

Code: Select all

#NoEnv
EnvGet, username, username 
msgbox % username
path := "C:\Users\" username "\AppData\Roaming\Code\User"
Run, %path%
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Using environment variables

26 Oct 2019, 03:47

Take a look at built in variables:
A_AppData
A_UserName

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ShatterCoder and 76 guests