Batch file : skipping folders starting with _ in FOR loop

穿精又带淫゛_ 提交于 2020-05-14 07:07:08

问题


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

Is it possible to do this ?

@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

Thank you very much in advance for helping !


回答1:


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

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


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

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



来源:https://stackoverflow.com/questions/61408681/batch-file-skipping-folders-starting-with-in-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!