Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude multiple folders with Dir command

Tags:

batch-file

dir

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.

like image 211
Double Quotes Avatar asked Jul 25 '26 01:07

Double Quotes


2 Answers

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

F:\>
like image 110
blabb Avatar answered Jul 28 '26 07:07

blabb


@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

like image 42
Aacini Avatar answered Jul 28 '26 08:07

Aacini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!