11

I'm trying to write a simple batch script that copies some files from the %userprofile%\Documents directory. I'm testing the script on my machine, but after some failures I realized that I moved my user's data to a separate partition. Therefore, %userprofile%\Documents is empty, and (obviously) nothing is copied.

I already tried other environment variables such as %homedrive% and %homepath%, but they still point at the standard C:\...

Is there a way to find out the actual drive used for the user's data?

I'm using Windows 10.

Thanks!

EDIT

As pointed out by Ƭᴇcʜιᴇ007, I didn't move the user's data to another partition, but I just relocated the "main" folders (Documents, Videos, ...) to some corresponding folders on another partition: right-click on (e.g.) Documents, Properties, Path.

How can I find out the path of a Document folder which has been relocated in this way?

5
  • 2
    %userprofile% should have the right folder regardless of where you put it, as long as you relocated the profile folders correctly it should be accurate. So how exactly did you move it to the other partition? Oct 6, 2016 at 21:43
  • Ok, I honestly didn't remember, so I browsed a bit and I finally found out what I did. I right-clicked on the "main" folders (Documents, Videos, etc..), selected Properties, selected the Path tab and changed the path. (I'm using the italian version of Windows, and as I'm barely translating the names I see on my screen, some words could be different). So you were right, %userprofile% is acutally doing nothing wrong. But the question is still: how can I find the actual path of the "main" Documents folder?
    – Alessandro
    Oct 6, 2016 at 21:53
  • Then I think you're confusing your Documents "library" with the Documents folder in your user profile path. Two different things... Oct 6, 2016 at 21:55
  • Is there a way to find out the path of the Documents folder, considering that it has been relocated in this way?
    – Alessandro
    Oct 6, 2016 at 22:04
  • Just for your consideration: this is really easy using Powershell: $temp = [Environment]::GetFolderPath('MyDocuments')
    – Cadoiz
    Aug 2, 2021 at 13:04

4 Answers 4

7

Unfortunately, there's not a simple environment variable you can check from a batch script if you've redirected this from the default path relative to your profile. You can see the full list of environment variables (including some undocumented) here:

http://ss64.com/nt/syntax-variables.html

Another thing to keep in mind is folder redirection even allows you to move these to a network share. There doesn't have to be a "drive" you can use all; it might just be a UNC path.

The good news is the information you want is available at the following registry location:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Personal

There's a whole set of folders you can move around in that User Shell Folders key, but to read the My Documents folder here using Windows Batch looks like this:

reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Personal

On my machine this produces the following output, which may be more or less useful to you depending on what you need and how good you are with Windows Batch scripts:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
    Personal    REG_EXPAND_SZ    C:\Users\joel\Documents

You can also try vbscript:

Set wshshell = CreateObject("WScript.Shell")
Documents = wshShell.SpecialFolders("MyDocuments")

or Powershell:

[Environment]::GetFolderPath('MyDocuments')

This may eventually be available via bash on Windows, too (really!) via WSL, but I haven't seen how to do it yet in a way that will be consistently accurate.

5

Assuming you have write access to either the current folder or some known-extant location on the computer... you can use PowerShell!

The Environment.GetFolderPath function can get the file system path of well-known locations, including the Documents folder. This PowerShell command prints the Documents path:

[Environment]::GetFolderPath('MyDocuments')

It can be written to a file with the Out-File cmdlet. The redirection operator > doesn't really work here because it produces a Unicode text file with a BOM, which is difficult to work with in the batch processor.

[Environment]::GetFolderPath('MyDocuments') | Out-File 'docspath.tmp' -Encoding ascii

That file can be read into a batch variable with set /p. Putting it all together, we get this batch script fragment:

powershell -Command "[Environment]::GetFolderPath('MyDocuments') | Out-File 'docspath.tmp' -Encoding ascii"
set /p DOCSPATH=< docspath.tmp
del docspath.tmp

The path to the user's Documents folder can now be expressed as %DOCSPATH%.

1
  • This should be the accepted answer since it is simple and above all, readable. Any kind of shell files get unreadable quickly if you are not careful. Jan 11, 2021 at 16:04
1

I have the reliable answer. Combination of powershell that returns the answer to dos

for /f "delims=" %%a in ('powershell.exe -command "& {write-host $([Environment]::GetFolderPath('MyDocuments'))}"') do Set "$Value=%%a"
Echo Value received from Powershell : %$Value%
1
  • I did not know that you can use a $ in a CMD variable name. But you know that it’s an uncommon usage, right? Mar 11, 2019 at 18:43
0

Well it's pretty simple :)

Location of desktop folder is listed here:

\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders

or even simplier in PowerShell

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

You must log in to answer this question.

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