以下代码示例提供了一种检索您需要的配置文件名称的方法,(那些不是特殊帐户且名称不以下划线开头的),以及它们当前的配置文件路径.
@For /F "Skip=1Tokens=1,2" %%G In ('%__AppDir__%wbem\WMIC.exe UserAccount Where^
"LocalAccount='True' And Not Name Like '[_]%%'" Get Name^,SID 2^>Nul'
)Do @For /F %%I In ("%%H")Do @For /F "Tokens=2Delims==" %%J In ('
%__AppDir__%wbem\WMIC.exe Path Win32_UserProfile Where^
"SID='%%I' And Special!='True'" Get LocalPath /Value 2^>Nul'
)Do @For /F "Tokens=*" %%K In ("%%J")Do @Echo User name:"%%G",Profile path:"%%K"
@Pause
虽然上述内容不能直接帮助您完成任务,但可以非常简单地对其进行调整。 (如果您在配置文件路径和目标目录之间复制/移动对象,它甚至还为您提供了使用 %%K 的机会。):
@Set "Target=D:\backup"
@For /F "Skip=1Tokens=1,2" %%G In ('%__AppDir__%wbem\WMIC.exe UserAccount Where^
"LocalAccount='True' And Not Name Like '[_]%%'" Get Name^,SID 2^>Nul'
)Do @For /F %%I In ("%%H")Do @For /F "Tokens=2Delims==" %%J In ('
%__AppDir__%wbem\WMIC.exe Path Win32_UserProfile Where^
"SID='%%I' And Special!='True'" Get LocalPath /Value 2^>Nul'
)Do @For /F "Tokens=*" %%K In ("%%J")Do @If Exist "%Target%\%%G\" (
Rem …your code here
)
@Pause
如果您的用户名中可能包含空格,那么答案会变得更加复杂。如果您确定在不是 Windows 7 的系统上运行它,它可以更简单地完成,但无论您使用哪种支持的操作系统,它都应该工作。
@Set "Target=D:\backup"
@For /F Tokens^=4Delims^=^" %%G In ('%__AppDir__%wbem\WMIC.exe UserAccount^
Where "LocalAccount='TRUE' And Not Name Like '[_]%%'" Assoc:List^
/ResultRole:SID 2^>NUL')Do @For /F Tokens^=1* %%H In (
'%__AppDir__%wbem\WMIC.exe UserAccount Where "Name='%%G'" Get SID^
/Value 2^>NUL^|%__AppDir__%find.exe "="')Do @For %%I In (%%H
)Do @For /F "Tokens=1*Delims==" %%J In (
'%__AppDir__%wbem\WMIC.exe Path Win32_UserProfile Where^
"SID='%%I' And Special!='TRUE' And LocalPath Is Not Null" Get LocalPath /Value^
2^>NUL^|%__AppDir__%find.exe "="')Do @For /F "Tokens=*" %%L In ("%%K"
)Do @If Exist "%Target%\%%G\" (
Rem …your code here
)
@Pause
在本例中,您的用户配置文件路径将分配给 %%~L(与上例中的 %%K 相对)。