使用Windows命令行检索所有用户的HOMEPATH

时间:2018-07-06 16:19:09

标签: windows batch-file

我想检索所有用户名的HOMEPATH目录(用户可能已手动将其更改为任何路径)。我的方法是尝试使用以下脚本从注册表中获取它们:

@echo off
REM This is to change codification, so the commands can work with international characters paths 
chcp 1252 > NUL 

REM Check Windows Version (only needed to check if its Windows XP or not)
ver|findstr " 5." > NUL

set RESULT=%ERRORLEVEL%

if %RESULT% == 0 (set VERSION=XP
) else (
    set VERSION=7
)

REM Retrieve Public Desktop directory from environment variables
IF %VERSION%==XP (
    set PUBLICDESKTOPDIR=%ALLUSERSPROFILE%\Desktop
    set USERSDIR=C:\Documents and Settings
) ELSE (
    set PUBLICDESKTOPDIR=%PUBLIC%\Desktop
    set USERSDIR=C:\Users
)

REM Copy configuration files to Cygwin home directory to all users in the system, so if setup is run under another user, it will have the files too
SET USERS="%USERNAME%"
SET USERSHOMEFOLDERS="%HOMEPATH%"
REM Command 'SETLOCAL ENABLEDELAYEDEXPANSION' is necessary so variable substitution with ! works (so variable susbsitution works correctly in for loops)
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /D %%I in ("%USERSDIR%\"*) DO (  
    IF NOT "%%~nI" == "Public" (
        IF NOT "%%~nI" == "cyg_server" (
            IF NOT "%%~nI" == "Default User" (  REM Default User is in Windows XP
                IF NOT "%%~nI" == "All Users" (  REM All Users is in Windows XP
                    IF NOT "%%~nI" == "%USERNAME%" ( REM Skip current user as the data can be gathered from the user environment variables
                        SET USERS=!USERS! "%%~nI"
                        REM Load user data into a temporay folder into the registry
                        reg load HKU\Temp_data "!USERSDIR!\%%~nI\NTUSER.DAT"

                        REM Look for HOMEPATH variable in Volatile Environment folder
                        FOR /F "tokens=2*" %%A IN ('REG QUERY "HKU\Temp_data\Volatile Environment" /v "HOMEPATH"^|FIND/I "HOMEPATH"') DO SET HOMEDIRTMP=%%B
                        REM Check if HOMEDIRTMP is empty
                        IF "!HOMEDIRTMP!"=="" (
                            SET HOMEDIRTMP=!USERSDIR!\%%~nI
                        )
                        SET USERSHOMEFOLDERS=!USERSHOMEFOLDERS! "!HOMEDIRTMP!"
                        REM Unload temporary data
                        reg unload HKU\Temp_data
                    )
                )               
            )
        )
    )
)
SETLOCAL DISABLEDELAYEDEXPANSION

问题是仅为当前用户加载“ Volatile Environment”文件夹。在注册表中加载“ NTUSER.DAT”时,此文件夹不存在,并且HOMEPATH变量未加载到其他任何位置。有没有办法从注册表或其他地方获取所有用户的HOMEPATH值?

1 个答案:

答案 0 :(得分:0)

在@eryksun的帮助下,它评论了使用net命令的可能性,我更改了脚本,以便它使用此命令代替注册表(这更好,因为最好不要加载和加载)。卸载注册表中的内容。

最终代码如下:

@echo off
REM This is to change codification, so the commands can work with international characters paths 
chcp 1252 > NUL 

REM Check Windows Version (only needed to check if its Windows XP or not)
ver|findstr " 5." > NUL

set RESULT=%ERRORLEVEL%

if %RESULT% == 0 (
    set VERSION=XP
) else (
    set VERSION=7
)

REM Retrieve Public Desktop directory from environment variables
IF %VERSION%==XP (
    set PUBLICDESKTOPDIR=%ALLUSERSPROFILE%\Desktop
    set USERSDIR=C:\Documents and Settings
) ELSE (
    set PUBLICDESKTOPDIR=%PUBLIC%\Desktop
    set USERSDIR=C:\Users
)

REM Copy configuration files to Cygwin home directory to all users in the system, so if setup is run under another user, it will have the files too
SET USERS=
SET USERSHOMEFOLDERS=
REM Command 'SETLOCAL ENABLEDELAYEDEXPANSION' is necessary so variable substitution with ! works (so variable susbsitution works correctly in for loops)
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /D %%I in ("%USERSDIR%\"*) DO (  
    IF NOT "%%~nI" == "Public" ( 
        IF NOT "%%~nI" == "cyg_server" (
            IF NOT "%%~nI" == "Default User" (  REM Default User is in Windows XP
                IF NOT "%%~nI" == "All Users" (  REM All Users is in Windows XP
                    REM Append to USERS variable the new user
                    SET USERS=!USERS! "%%~nI"
                    FOR /F "tokens=2*" %%A IN ('net user "%%~nI"^|find "Home directory"') DO SET HOMEDIRTMP=%%B
                    REM If HOMEDIRTMP is empty, then set the default homepath directory
                    IF "!HOMEDIRTMP!"=="" (
                        SET HOMEDIRTMP=!USERSDIR!\%%~nI
                    )
                    REM Append to USERSHOMEFOLDERS vriable the new user's homepath
                    SET USERSHOMEFOLDERS=!USERSHOMEFOLDERS! "!HOMEDIRTMP!"
                )               
            )
        )
    )
)
SETLOCAL DISABLEDELAYEDEXPANSION