Exclude multiple folders with Dir command

拈花ヽ惹草 提交于 2020-01-05 10:10:54

问题


I had search thru the forum with excluding multiple files while using DIR command. But didn't find what i really needed. Some are even with PHP or VBS, really don't know how they works.

What I'm trying to do is *DIR*ing all profile folders and also excluding certain default folders, this is a part of showing what and how many users are having profiles on that computer.

I will explain a bit more because i may not fully be understood what my needs are.

If i use the DIR without findstr.

DIR /A:d /B "F:\Documents and Settings"

Output:

Administrator
All Users
Default User
LocalService
userprofile1
NetworkService
userprofile2
...
userprofileN

But my actual needs are:

userprofile1
userprofile2
...
userprofileN

I also got this far with my script now.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
IF ERRORLEVEL 1 ECHO Your OS does not support enable extensions
DIR /A:d /B "F:\Documents and Settings" | findstr /v "Admin" | findstr /v "Default" | findstr /v "LocalService" | findstr /v "NetworkService"
EXIT

My above script actually works but is fixed and may not be pretty coded. I would ask about is it possible to use a For loop to shorten the long findstr pipes and set a variable that contains the folder names to exclude ? I prefer to use a variable instead of a text files that contains folders to exclude.

I came up with below but it duplicates the ouput:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
IF ERRORLEVEL 1 ECHO Your OS does not support enable extensions
SET xUser="Admin" "Default" "hi_I_will_be_future_added"
FOR %%A IN (!Xuser!) DO ( 
 DIR /A:d /B "F:\Documents and Settings" | findstr /v %%A
)

Thank you to this great forum and for all that may had helped both me and others.


回答1:


@echo off
setlocal EnableDelayedExpansion

set exclude=/Administrator/All Users/Default User/LocalService/NetworkService/
for /F "delims=" %%a in ('DIR /A:d /B "F:\Documents and Settings"') do (
   if "!exclude:/%%~Na/=!" equ "%exclude%" echo %%a
)

If you want to show the user name only (with no "F:\Documents and Settings" path), use echo %%~Na




回答2:


Splitted in several lines for better reading, but, of course, it can be in a single line

@echo off
    dir /ad /b "c:\Documents And Settings" ^
    | findstr /l /i /x /v   ^
        /c:"Administrator"  ^
        /c:"All Users"      ^
        /c:"Default User"   ^
        /c:"LocalService"   ^
        /c:"NetworkService" 

Or, instead of passing all the strings as /c:"..." arguments, you can generate a file with all the strings to exclude and use the /g:filename switch of findstr




回答3:


F:\>dir /AD /B "c:\documents and settings"| FINDSTR /V "All Default Local Networ
k"
Admin
Administrator

F:\>


来源:https://stackoverflow.com/questions/24127017/exclude-multiple-folders-with-dir-command

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