批处理文件:跳过 FOR 循环中以 _ 开头的文件夹
Batch file : skipping folders starting with _ in FOR loop
我想排除所有以 _ 开头的配置文件,而不必在排除文本文件中列出每个配置文件。
可以这样做吗?
@echo off
set Target=D:\backup
for /f "tokens=*" %%I in ('dir /a:d-h /b "%SystemDrive%\Users\*"') do if exist "%Target%\%%~nXI\" (
........
)
pause
exit
非常感谢您的帮助!
以下代码示例提供了一种检索您需要的配置文件名称的方法,(那些不是特殊帐户且其名称不以下划线开头的名称),以及以及他们当前的个人资料路径。
@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
虽然以上内容不能直接帮助您完成任务,但可以非常简单地对其进行调整,从而做到这一点。 (如果您是配置文件路径和目标目录之间的 copying/moving 个对象,它甚至还为您提供了使用 %%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 的系统上,它可以更简单地完成,但是无论您使用的是哪个支持 OS,这都应该有效。
@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
相对)。
我想排除所有以 _ 开头的配置文件,而不必在排除文本文件中列出每个配置文件。
可以这样做吗?
@echo off
set Target=D:\backup
for /f "tokens=*" %%I in ('dir /a:d-h /b "%SystemDrive%\Users\*"') do if exist "%Target%\%%~nXI\" (
........
)
pause
exit
非常感谢您的帮助!
以下代码示例提供了一种检索您需要的配置文件名称的方法,(那些不是特殊帐户且其名称不以下划线开头的名称),以及以及他们当前的个人资料路径。
@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
虽然以上内容不能直接帮助您完成任务,但可以非常简单地对其进行调整,从而做到这一点。 (如果您是配置文件路径和目标目录之间的 copying/moving 个对象,它甚至还为您提供了使用 %%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 的系统上,它可以更简单地完成,但是无论您使用的是哪个支持 OS,这都应该有效。
@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
相对)。