使用 Dir 命令排除多个文件夹

Dou*_*tes 5 batch-file dir

我在使用 DIR 命令时通过论坛进行了搜索,排除了多个文件。但没有找到我真正需要的东西。有些甚至用PHP或VBS,真的不知道它们是如何工作的。

我想做的是*DIR*ing所有配置文件文件夹,并排除某些默认文件夹,这是显示该计算机上有哪些用户以及有多少用户拥有配置文件的一部分。

我会解释更多,因为我可能不完全理解我的需求是什么。

如果我使用不带findstr的DIR

DIR /A:d /B "F:\Documents and Settings"
Run Code Online (Sandbox Code Playgroud)

输出:

Administrator
All Users
Default User
LocalService
userprofile1
NetworkService
userprofile2
...
userprofileN
Run Code Online (Sandbox Code Playgroud)

但我的实际需求是:

userprofile1
userprofile2
...
userprofileN
Run Code Online (Sandbox Code Playgroud)

现在我的脚本也已经完成了。

@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
Run Code Online (Sandbox Code Playgroud)

我的上面的脚本实际上可以工作,但是是固定的,并且可能没有很好的编码。我想问是否可以使用For循环来缩短长findstr管道并设置一个包含要排除的文件夹名称的变量?我更喜欢使用变量而不是包含要排除的文件夹的文本文件。

我想出了下面的方法,但它重复了输出:

@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
)
Run Code Online (Sandbox Code Playgroud)

感谢这个伟大的论坛以及所有可能对我和其他人有所帮助的人。

Aac*_*ini 2

@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
)
Run Code Online (Sandbox Code Playgroud)

如果您只想显示用户名(没有“F:\Documents and Settings”路径),请使用echo %%~Na