Is there any environment variable that represents

2020-07-09 07:26发布

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.

5条回答
Explosion°爆炸
2楼-- · 2020-07-09 08:00

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)
查看更多
Explosion°爆炸
3楼-- · 2020-07-09 08:12

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.

查看更多
该账号已被封号
4楼-- · 2020-07-09 08:16

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

查看更多
来,给爷笑一个
5楼-- · 2020-07-09 08:16

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

%USERPROFILE%\..
查看更多
爱情/是我丢掉的垃圾
6楼-- · 2020-07-09 08:20

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.

查看更多
登录 后发表回答