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).
User Home Path environment variable Topic is solved
Re: User Home Path environment variable
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 !
Re: User Home Path environment variable Topic is solved
EnvGet should have you covered if you use #NoEnv (recommended):
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.
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"
Without #NoEnv, you could even use hdrive := HomeDrive and hpath := HomePath.
Re: User Home Path environment variable
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:
Thanks very much for helping me solve this.
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"
Who is online
Users browsing this forum: DIYCB, Google [Bot] and 43 guests