使用Dir命令排除多个文件夹 [英] Exclude multiple folders with Dir command
问题描述
使用DIR命令时,我在论坛中搜索了多个文件,但排除了多个文件.但是没有找到我真正需要的.有些甚至与PHP或VBS一起使用,真的不知道它们如何工作.
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.
我要尝试的是对所有配置文件文件夹进行 * DIR * ing 操作,并且还要排除某些默认文件夹,这是显示在该计算机上拥有什么配置文件的用户数量和数量的一部分.
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.
如果我在没有 findstr 的情况下使用 DIR .
If i use the DIR without findstr.
DIR /A:d /B "F:\Documents and Settings"
输出:
Administrator
All Users
Default User
LocalService
userprofile1
NetworkService
userprofile2
...
userprofileN
但是我的实际需求是:
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
我上面的脚本实际上可以工作,但是已经修复,可能没有很好的编码. 我会问是否可以使用 For 循环来缩短长的 findstr 管道并设置一个包含要排除的文件夹名称的变量? 我更喜欢使用变量而不是包含要排除的文件夹的文本文件.
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.
推荐答案
@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
)
如果只想显示用户名(不显示"F:\ Documents and Settings"路径),请使用echo %%~Na
If you want to show the user name only (with no "F:\Documents and Settings" path), use echo %%~Na
这篇关于使用Dir命令排除多个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!