'How to create a dynamic local file path that changes based on the windows user

I have created a local windows filepath that is dynamic based on the user.

C:\Users\%USERPROFILE%\rest_of_filepath

This path works perfectly on my local machine, but when I email it to someone else, they get this error:

We can't find 'C:\Users\%USERPROFILE%\rest_of_path'. Please make sure you are suing the correct location or web address.

How to I get it to work for the others I am sending it to?



Solution 1:[1]

The USERPROFILE is the the correct Environment Variable to use to retrieve the path of the Windows user profile folder. This will work even if the folder is in a "non-default" location. Windows created this Environment Variable for this exact reason. Encasing a word in % informs Windows that the word inside the % is an Environment Variable. Windows therefore knows to replace the entire '%USERPROFILE%' with the path associated to that variable.

In the case of the question the path C:\Users\%USERPROFILE%\rest_of_filepath is used. The C:\Users\ portion of this path is unnecessary and results in C:\Users\C:\Users\<userprofilefolder>\rest_of_path since the Environment Variable will also retrieve that portion of the path.

%USERPROFILE\rest_of_path is all that is needed.

Ultimately, even if the HTML interpreter in the email client was able to parse Environment Variables, access to local system files from email is not allowed for security reasons.

Emailing the path and instructing the recipient to copy and paste the it into the Windows file explorer is the recommended work-around.

src: Recognized Environment Variables that are recognized only in the user context

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1