批处理文件:在FOR循环中跳过以_开头的文件夹 [英] Batch file : skipping folders starting with _ in FOR loop

查看:322
本文介绍了批处理文件:在FOR循环中跳过以_开头的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想排除所有以 _ 开头的个人资料,而不必在排除文本文件中列出每个个人资料.

I would like to exclude all profiles starting with _ without having to list each profile in an exclusion text file.

可以这样做吗?

@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

非常感谢您的帮助!

推荐答案

下面的代码示例提供了一种检索所需的配置文件名称的方法,(不是特殊帐户,并且名称不以其开头的名称) (下划线),以及它们当前的配置文件路径.

The following code example provides a methodology for retrieving the profile names you require, (those which are not a special account and whose names do not begin with an underscore), together with their current profile paths.

@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的机会.):

Whilst the above does not directly help you with your task, it could very simply be adapted, to do so. (It even affords you the opportunity to use %%K too, should you be copying/moving objects between the profile path and the target directory.):

@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,它都可以正常工作.


If there's a possibility that you have user names containing spaces, the answer becomes a little more involved. It could be done more simply if you were definitely running this on systems which weren't Windows 7, but this should work regardless of which supported OS you're using.

@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相对.).

In this example, your user profile path will be assigned to %%~L, (as opposed to %%K, in the previous example.).

这篇关于批处理文件:在FOR循环中跳过以_开头的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆