User Home Path environment variable Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Lem2001
Posts: 63
Joined: 27 Jun 2017, 17:59

User Home Path environment variable

11 May 2020, 05:47

I need to delete a file from the Home folder of the current user.

Windows has the environment variables %HomeDrive% and %HomePath% that can be used to construct the correct path, but AHK has no equivalent A_HomeDrive and A_HomePath.

What is the correct syntax to use for a FileDelete command if I wanted to delete say, <drive>:\Users\<current user>\junk.txt regardless of which drive or location the user's Home folder is in?

I tried to use EnvGet, but this whole wretched business with legacy versus expression paths means that I don't know how to put the command together (which bits to enclose in quotes, which bits to leave out, whether to use % or not, etc.). I tried numerous variations, but none of them worked.

I know that its a problem with constructing the path because if I forego the variable aspect and just enter an actual path, then the file does delete, but that will obviously not work for any other accounts (because the current username would be hard-coded into the FileDelete command).
BNOLI
Posts: 548
Joined: 23 Mar 2020, 03:55

Re: User Home Path environment variable

11 May 2020, 06:04

MsgBox % USERPROFILE :?:
Remember to use [code]CODE[/code]-tags for your multi-line scripts. Stay safe, stay inside, and remember washing your hands for 20 sec !
gregster
Posts: 4652
Joined: 30 Sep 2013, 06:48

Re: User Home Path environment variable  Topic is solved

11 May 2020, 06:11

EnvGet should have you covered if you use #NoEnv (recommended):

Code: Select all

#NoEnv
EnvGet, hdrive, Homedrive
EnvGet, hpath, Homepath
msgbox % hdrive "`n" hpath
msgbox % hdrive ":\Users" hpath "\junk.txt"
; FileDelete % hdrive ":\Users\" hpath "\junk.txt"
Same goes for UserProfile, like recommended by BNOLI: FileDelete % UProfile "\junk.txt" (get the userprofile via EnvGet again)

Without #NoEnv, you could even use hdrive := HomeDrive and hpath := HomePath.
Lem2001
Posts: 63
Joined: 27 Jun 2017, 17:59

Re: User Home Path environment variable

11 May 2020, 06:54

Hi gregster,

This works great, although you added ":\Users" which breaks the command (because the Homepath variable already contains the full path minus the drive letter).

Therefore, the command that works is this:

Code: Select all

EnvGet, hdrive, Homedrive
EnvGet, hpath, Homepath
FileDelete % hdrive hpath "\junk.txt"
Thanks very much for helping me solve this.
gregster
Posts: 4652
Joined: 30 Sep 2013, 06:48

Re: User Home Path environment variable

11 May 2020, 07:02

Lem2001 wrote:
11 May 2020, 06:54
This works great, although you added ":\Users" which breaks the command (because the Homepath variable already contains the full path minus the drive letter).
Oh yeah, sorry, that was wrongly copypasted :shifty: and not really tested.
But good to know that it helped!

Return to “Ask For Help”

Who is online

Users browsing this forum: anhnha, Bing [Bot], pnKed, SilasDeVis and 61 guests