4

I have a batch script that need the full path of the Desktop folder.

Right now I am using %USERPROFILE%/Desktop, it works fine until I learned that the path can be changed (See Can you change the location of the Desktop folder in Windows?).

I tried to read the regedit and I end up with something like this (I have cygwin installed):

REG QUERY "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\Current
Version\Explorer\Shell Folders" /v Desktop |grep Desktop| awk '{$1=$2=""; print $0}'

This is working fine but I don't like it for the sake of robust and simplicity. Not to metion that it will be even more messy if I don't have awk avaliable.

As I noticed, you can type shell:Desktop in File Explorer and it will open the Desktop folder for you. However I have no clue how I can use that in a batch file.

Is there a hidden environment variable or simple command I can use to retrive this value?

1 Answer 1

8

There are no environment variables for the majority of special folders. The official method is to call either SHGetKnownFolderPath() from Win32 API or System.Environment.GetFolderPath() from .NET – the latter is easily accessible through PowerShell:

$dir = [Environment]::GetFolderPath("Desktop")

Batch scripts (Cmd.exe) do not have access to such features; you would have to call a program that does (such as PowerShell or third-party tools like nircmd).

1
  • This is the way to go: it "survives" the poor implementation of OneDrive's backup functionality. (If you decide to backup your Desktop with OneDrive, it moves the folder from its original location to a magically maintained folder of its choosing.)
    – Granger
    Jun 15, 2022 at 21:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .