reference %USERPROFILE% in a shortcut when the variable has a hidden re-reference

8,789

Solution 1

I resolved my problem by using SETX to create two new environment variables: myUSERPROFILE and myHOMEDRIVE. These are used in shortcuts which point to my cloud based data on OneDrive, Dropbox, GoogleDrive, etc.

USE CASE

The purpose is to abstract the shortcuts from the local machine to the generality of all machines I work on.

Specifically, I have a SurfacePro 4, a desktop, a laptop (dedicated to the media center via it's amp) and then the laptops clients give me to work on from time to time.
Each of those have different USER names and home drives determined by the client network lot.
On my desktops I have 4 shortcuts pointing to folders in the cloud ~ Incoming images; Reading; Quick Reference; Incoming documents. The shortcuts take the form

"%myUSERPROFILE%\Documents\Dropbox (Work)\Incoming docs"

Whichever shortcut I click on, irrespective of the computer I happen to be at opens the cloud-based folder.
Dropping a file on the shortcut folder sends it to the right place.
Some of the folders thenselves have shortcuts pointing to the files or folders in my data repositories.
The Quick Reference folder, for example, has shortcuts to everything from schedules and timetables to technical data to Client protocols.
All these stay permanently in their various data repositories - There are north of 3500 pdfs alone in my tech libraries. (I also use calibre to generate meta data).

PROCESS

create the variables

When I get a new machine to work on, this batch file runs in Admin mode:

:: setEnvVariables.bat
:: Step 1, set variables
echo Enter the home drive, default is %HOMEDRIVE%
SET /P localHOMEDRIVE=%HOMEDRIVE%
echo Enter the User prefix, default is %USERPROFILE%
SET /P localUSERPROFILE=%USERPROFILE%
SETX myHOMEDRIVE localHOMEDRIVE
SETX myUSERPROFILE localUSERPROFILE

echo: Step 2. check that they work
echo: =====: flip drives, land on home data drive
C:
echo: =====: directory of folders in root C:\ drive
DIR /b /ad
PAUSE
%myHOMEDRIVE%
cd\
echo: =====: directory of folders in root data drive
DIR /b /ad
PAUSE
cd %myUSERPROFILE%\Documents
echo: =====: directory of folders in Documents folder
DIR /b /ad
PAUSE
echo: ====: directory of folders in Dropbox folder
cd %myUSERPROFILE%\Documents\Dropbox (Personal)
DIR /b /ad
PAUSE
:: ...etc all the other cloud based repositories. 

A typical shortcut in the Reading folder would be

"%OneDrive%\My Digital Editions\~ Knowledge\Consultancy\Business\Big Data_CDI_MDM\Path-to-Predictive-Analytics-and-Machine-Learning.pdf"

Or I might just drop the file in the folder as explained above, and file it in the appropriate repository (folder path) if I think it's worth keeping.
When I hand client machines back, I delete the two Environment variables . . . of course!

create the Shortcut folders

I keep a folder in the cloud with the above batch file in it, and master shortcuts to these folder shortcuts.

These are just dragged to the desktop and dropped there.

Since these are just shortcuts, I think of them as "virtual folders" or "virtual desktops". No data ever sits on the desktop anymore. If I need a special folder (for example for a client project) I simply create a shortcut to the folder in the cloud and the environment variables take over from there.

Now, I'm ready to go on the new computer.

Solution 2

Open a PowerShell window and run:

[Enum]::GetNames([Environment+SpecialFolder]) | ? {$_ -like "My*" } | % { $_ + ": " + [Environment]::GetFolderPath($_)}

if it returns something like:

MyDocuments: H:\...
MyMusic: H:\...
MyVideos: H:\...
MyComputer: 
MyPictures: H:\...

then someone has changed the location of these folders, you can change them back by opening the properties for these folders in File Explorer and click on the Location tab.

To use the correct location of MyDocuments in a shortcut you can use:

explorer.exe shell:::{A8CDFF1C-4878-43be-B5FD-F8091C1C60D0}\folder1\folder2

this will open H:\documents\folder1\folder2 in File Explorer.

All of the special folders have a unique GUID, a good list is at TenForums

I don't think you can use these GUIDs directly in cmd.exe, but in PowerShell you can do:

cd $([Environment]::GetFolderPath('MyMusic'))
Share:
8,789

Related videos on Youtube

ilinkcs
Author by

ilinkcs

Updated on September 18, 2022

Comments

  • ilinkcs
    ilinkcs over 1 year

    Win10, although I expect it applies equally from 7up.

    if I echo %USERPROFILE% it returns a drive letter of C:\ ~ That's correct.

    However, my Documents, Pictures, Music, etc. folders are on the H: drive. Somehow, Win10 "knows" this, because it redirects away from the C: drive.

    In the past, I would use %USERPROFILE% to reference files along the folder tree (e.g. in shortcuts) but now that I've got a split drive system I don't know how to reference the re-direct instead.

    Any ideas?

  • ilinkcs
    ilinkcs over 7 years
    thank you . . . I tried replicating the old Win7 God mode by using (eg) pdm.{FDD39AD0-238F-46AF-ADB4-6C85480369C7} which I got off Technet - known folder ID. It dropped me into the documents folder but won't take me down the tree into (eg) \pdm. both explorer shell:::{CLSID key}\folder1\folder2 -and- explorer /e,::{CLSID key}\folder1\folder2 do the same (ie only open the Documents folder) At least I'm getting into the H: drive, though!! even if the whole path isn't being built. is there a concatenation problem do you think?
  • Peter Hahndorf
    Peter Hahndorf over 7 years
    On my Windows 10 linking into folders under documents works fine, just to make sure folder1 and folder2 and example names which need to be replaced with your real names.
  • ilinkcs
    ilinkcs over 7 years
    Hi ~ yes, and thanks for the help so far. . Say I have a file <readme.txt> in a folder <\PDM> coming off of the Documents folder. Then from the command prompt (testing the shortcut): <explorer.exe shell:::{A8CDFF1C-4878-43be-B5FD-F8091C1C60D0}\PDM\> (without the brackets) should open the folder, and <explorer.exe shell:::{A8CDFF1C-4878-43be-B5FD-F8091C1C60D0}\PDM\readme.tx‌​t> should open the file. In the former case, whoohoo! the folder opens in explorer. In the second case, the documents folder will open-Win10 is rejecting the suffix portion (folder/file) when there is a file appended.
  • ilinkcs
    ilinkcs over 7 years
    Same thing happens in the shortcut. Include just the folder command and the correct folder opens. Append the file name and it all falls down. I tried to place the reference into "Start in folder", but it doesn't do the interpretation and falls over on a "invalid folder" type error. Odd. Very.
  • ilinkcs
    ilinkcs over 4 years
    an added advantage is the console batch files I use at the command line use these variables as well. So all the clean-up, maintenance etc. batches are also insulated from the differeing HOMEDRIVE and USERPROFILE variables.