batch-file - 批处理文件删除 C:\users\*name* 指定文件夹除外
问题描述
我正在尝试编写一个批处理文件来删除 c:\users 中的所有文件夹,但有一些例外。
场景:我们在一个库中有多台共享 PC,用户可以登录并且不再使用这些 PC。这通常会用用户配置文件填满 c:\ 驱动器。
我想从 c:\users 中删除除 c:\users\defaultuser0 和 c:\users\public 之外的所有文件夹。
希望这是有道理的?我看过其他一些问题/答案,但它们只指定如何指定 1 个文件夹。另外,我知道 delprof,因为我们确实在其他应用程序中使用它,但我想编写自己的文件,以便针对这种情况进行修改。
解决方案
如果您仍然想删除目录,我的建议是,我建议您WMIC
从批处理文件中这样使用:
@For /F "Tokens=1*Delims==" %%A In ('WMIC Path Win32_UserProfile Where "Special!='True' And LocalPath Is Not Null And Not LocalPath Like '%%\\defaultuser0'" Get LocalPath /Value 2^>Nul')Do @For /F "Tokens=*" %%C In ("%%B")Do @RD/S/Q "%%C"
如果您偏爱该Net User
方法,我仍然会WMIC
从批处理文件中建议,如下所示:
@For /F Tokens^=2^Delims^=^" %%A In ('WMIC Path Win32_UserProfile Where^
"Special!='True' And LocalPath Is Not Null" Assoc^
/AssocClass:Win32_UserAccount 2^>Nul')Do @For /F "Tokens=1*Delims==" %%B In ('
WMIC UserAccount Where^
"SID='%%A' And LocalAccount='TRUE' And Name!='defaultuser0'" Get Name /Value^
2^>Nul')Do @For /F Tokens^=* %%D In ("%%C")Do @Net User "%%D" /Delete
使用 WMI 检索用户帐户路径和/或名称比仅选择默认位置的目录和用户原始名称更可靠
推荐阅读
- python - 为我的数据框代码不断获取“TypeError:'numpy.ndarray'对象不可调用”
- http - 即使 curl 请求有效,Axios 和邮递员请求也不起作用
- css - 用于 Wordpress 主题的带有 CSS 的垂直弹出菜单
- python - 如何在文件中找到所有字符串子字符串然后对其进行操作?- Python
- redis - --scan --pattern *some_numbers* 找不到我的大部分密钥
- sql - MS Access:为什么我的错误信息在输入后立即出现?
- github-actions - 如何在 github 操作中引用环境机密
- node.js - mongoose.ObjectId 和 Schema.Types.ObjectId 的区别?
- python - 如何使用 conda develop 安装具有所有依赖项的脚本(在开发/可编辑模式下)?
- node.js - 使用 ocr 是否可以在图像中制作可点击按钮?