首页 > 解决方案 > 批处理文件删除 C:\users\*name* 指定文件夹除外

问题描述

我正在尝试编写一个批处理文件来删除 c:\users 中的所有文件夹,但有一些例外。

场景:我们在一个库中有多台共享 PC,用户可以登录并且不再使用这些 PC。这通常会用用户配置文件填满 c:\ 驱动器。

我想从 c:\users 中删除除 c:\users\defaultuser0 和 c:\users\public 之外的所有文件夹。

希望这是有道理的?我看过其他一些问题/答案,但它们只指定如何指定 1 个文件夹。另外,我知道 delprof,因为我们确实在其他应用程序中使用它,但我想编写自己的文件,以便针对这种情况进行修改。

标签: batch-file

解决方案


如果您仍然想删除目录,我的建议是,我建议您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 检索用户帐户路径和/或名称比仅选择默认位置的目录和用户原始名称更可靠


推荐阅读