admin管理员组

文章数量:1201790

Is there any environment variable or Other format that the profile path is represented in Windows? I want to query in such a way that I should get the value "C:\Documents and Settings (if windows XP or 2k3) or C:\users (If vista or windows 7).

I dont want the current user name appended to the string, which I can get thru %USERPROFILE% variable.

Is there any environment variable or Other format that the profile path is represented in Windows? I want to query in such a way that I should get the value "C:\Documents and Settings (if windows XP or 2k3) or C:\users (If vista or windows 7).

I dont want the current user name appended to the string, which I can get thru %USERPROFILE% variable.

Share Improve this question asked Dec 21, 2010 at 21:21 svvsvv 711 gold badge1 silver badge2 bronze badges 2
  • 3 As you can put USER Profiles anywhere e.g. you could set mark up as c:\mark why do you need this and not USERPROFILE ? Also note the last directory is not necessarily the same as the user name. – mmmmmm Commented Dec 21, 2010 at 21:25
  • Yes Mark.. What you said is true. I just want to know where a particular user profile is located? Also My environment doesn't 'redirect' these profiles other than default locations. – svv Commented Dec 21, 2010 at 21:34
Add a comment  | 

5 Answers 5

Reset to default 11

It doesn't exist. Instead, try %USERPROFILE%\..

Warning: as @Mark suggests, this is not reliable because the user profile directory may really be any arbitrary location.

On Vista+ you can use FOLDERID_UserProfiles to get C:\Users (or whatever it may be in localized versions, etc). On XP and earlier you'll pretty much have to go the CSIDL_COMMON_DESKTOPDIRECTORY route that will give you "C:\Documents and Settings\All Users\Desktop" and work your way back from there.

I think this settles it for Vista. For XP the solution is not perfect, but at least it won't depend on the current user's profile path. "All Users" will always exist, and I can't think of a reason for it to be in a place other than the default.

To the best of my knowledge no; but you can do a last instance of '/' to find the parent directory of %USERPROFILE%

Yeah there actually is a way to get it to work:

%USERPROFILE%\..

I derived the batch and VBS methods (below), since I couldn't find an equivalent batch or VBS method for this question anywhere else. If I shouldn't add it to this thread (jscript), please add a comment on how/where it should go, and I will delete this answer and post as directed. :)

Batch (single line - no carriage return):

for /f "tokens=2*" %%f in ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /v ProfilesDirectory ^|find /i "Profiles"') do @set ProfDir=%%g

VBScript:

' http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/registry/#ListRegFiles.htm

const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set StdOut = WScript.StdOut

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
oReg.EnumValues HKEY_LOCAL_MACHINE, strKeyPath,_
 arrValueNames, arrValueTypes

For i=0 To UBound(arrValueNames)
'    StdOut.WriteLine "File Name: " & arrValueNames(i) & " -- "
    oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,_
    arrValueNames(i),strValue
'    StdOut.WriteLine "Location: " & strValue
'    StdOut.WriteBlankLines(1)
    IF arrValueNames(i) = "ProfilesDirectory" THEN ProfileRoot= strValue
Next

wscript.echo("ProfileRoot=" & ProfileRoot)

本文标签: