批处理文件删除目录中除指定列表之外的所有文件夹

发布于 2024-12-05 22:38:17 字数 77 浏览 1 评论 0原文

我正在寻找一个批处理文件,该文件将进入 C:\Documents and Settings\ 并删除除我想保留的几个文件夹之外的所有文件夹。

I'm looking for a batch file that will go into C:\Documents and Settings\ and delete all folders except a few that I want to keep.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心舞飞扬 2024-12-12 22:38:17

这是一个 hack-around =D

如果你有一个文件夹路径列表,比如folders.txt,如下所示:

  • C:\Documents and Settings\Mechaflash
  • C:\Documents and Settings\Mom
  • C:\Documents and Settings\Dad

等。您可以做的就是暂时将它们更改为隐藏文件夹,然后将 RMDIR 更改为所有非隐藏文件夹。

CD "C:\Documents and Settings\"
FOR /F "tokens=*" %%A IN (folders.txt) DO (
 ATTRIB +H "%%A" /S /D
)
FOR /F "USEBACKQ tokens=*" %%F IN (`DIR /B /A:-HD "C:\Documents and Settings\"`) DO (
 RMDIR /S /Q %%A
)
FOR /F "tokens=*" %%A IN (folders.txt) DO (
 ATTRIB -H "%%A" /S /D
)

Here's a hack-around =D

If you have a list of folder paths in say folders.txt listed as so:

  • C:\Documents and Settings\Mechaflash
  • C:\Documents and Settings\Mom
  • C:\Documents and Settings\Dad

etc. What you can do is temporarily change them to hidden folders, then RMDIR on all non-hidden folders.

CD "C:\Documents and Settings\"
FOR /F "tokens=*" %%A IN (folders.txt) DO (
 ATTRIB +H "%%A" /S /D
)
FOR /F "USEBACKQ tokens=*" %%F IN (`DIR /B /A:-HD "C:\Documents and Settings\"`) DO (
 RMDIR /S /Q %%A
)
FOR /F "tokens=*" %%A IN (folders.txt) DO (
 ATTRIB -H "%%A" /S /D
)
还在原地等你 2024-12-12 22:38:17

使用 robocopy 的解决方案:

cd /d "C:\Documents and Settings"
md tmp
robocopy . tmp /E /MOVE /XD folderToKeep1 folderToKeep2 ...
rd /s /q tmp

A solution using robocopy:

cd /d "C:\Documents and Settings"
md tmp
robocopy . tmp /E /MOVE /XD folderToKeep1 folderToKeep2 ...
rd /s /q tmp
彩扇题诗 2024-12-12 22:38:17
rem the last space character is deliberate
set yourKeepList="abc def "
for /f %%f in ('dir /b/ad "C:\Documents and Settings"') do (
    (echo %yourKeepList% | findstr /v /i "%%f " 1>nul) && rd /q/s %%f
)
rem the last space character is deliberate
set yourKeepList="abc def "
for /f %%f in ('dir /b/ad "C:\Documents and Settings"') do (
    (echo %yourKeepList% | findstr /v /i "%%f " 1>nul) && rd /q/s %%f
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文