gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:47:35 25 4
gpt4 key购买 nike

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

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

我会解释得更多一些,因为我可能没有完全理解我的需求。

如果我使用没有 findstrDIR

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

输出:

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

但我的实际需求是:

userprofile1
userprofile2
...
userprofileN

现在我的脚本也走到了这一步。

@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 管道并设置一个包含要排除的文件夹名称的变量?我更喜欢使用变量而不是包含要排除的文件夹的文本文件。

我在下面想出了但它重复了输出:

@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
)

感谢这个伟大的论坛,感谢所有可能对我和其他人都有帮助的内容。

最佳答案

@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

关于batch-file - 使用 Dir 命令排除多个文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24127017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com