Batch to open a folder within a user's folder(c:\users\usernamehere\my documents)?
Asked Answered
T

2

26

But I am looking for a code that will allow whatever user that uses my batch file to get to their my documents. The idea is that they would type 3, press enter, and then the program would open up their "My Documents" folder.

Unfortunately every user has a different name, so the following command won't work unless it has a specific name after it.

%SystemRoot%\explorer.exe c:\users\James\My Documents

Is there some sort of replacement for the "James"? A universal command?

Touchy answered 28/1, 2012 at 23:16 Comment(1)
This question is also asked on ServerFault. This is the best answer, that also accounts for users who have moved their Documents special folder.Personate
S
44

You can use the %UserProfile% environment variable, which points directly to the personal folder of the logged in user:

%SystemRoot%\explorer.exe %UserProfile%\My Documents

-If you're using windows 7 just do explorer %UserProfile%\Documents (should work with other Windows but I haven't tried...)

this is because any exe in system root can be used as a command and to my knowledge most versions of windows have a folder called "Documents" not "My Documents" it just appears that way to the user (same for Music etc.)

Skip answered 28/1, 2012 at 23:18 Comment(2)
The My Documents part is not fixed. It varies with Windows versions and installation language.Moonraker
This doesn't work reliably. Windows lets users move their My Documents folder to other places, such that it isn't directly under their %UserProfile% location.Personate
T
1

11-year update:

After coming from middle-school Windows XP bash scripting to getting a CS degree and ending up in Windows 11 development, I've come to the same conclusion that others have already commented:

THERE IS NO RELIABLE WAY TO GET THIS FOLDER. IF YOU TRY TO YOU WILL EVENTUALLY GET ERRORS.

The folder path will vary from system-to-system and there's no reliable way to verify the folder is the same folder. It's often under %UserProfile%\Documents but THIS IS NO GUARANTEE AND YOU SHOULD NOT TRUST THIS.




POSSIBLE solutions

So you're desperate? Good luck finding a reliable solution across every version of Windows and every user. That said, I'll provide some aid if you absolutely positively truly have to get this folder for some reason:

  1. Your code should be testing for different paths to find it
  2. If it fails to do so, one alternative is to get your user to input it for you
  3. Alternatively, just use a different folder! %appdata% or %localappdata% are acceptable places, depending on your project

There are ways to get this folder but there's no guarantee on the reliability of any of these.

  • PowerShell: [Environment]::GetFolderPath("MyDocuments") (enum)
  • C#: Environment.SpecialFolder.MyDocuments (enum)
  • Variety of registry commands:
reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User
Shell Folders" /v personal

reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell 
Folders" /v personal
Touchy answered 19/3, 2023 at 2:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.