删除除一个以外的所有文件夹 [英] Delete all folders except one

查看:78
本文介绍了删除除一个以外的所有文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要删除C:\ temp中的所有文件和文件夹,除了一个包含许多文件和子文件夹的特定文件夹(C:\ temp \ 123).

I want to delete all files and folders in my C:\temp except one specific folder (C:\temp\123) which contains a lot of files and subfolders.

我尝试使用 pushd"c:\ temp \ 123"&&rd/s/q"c:\ temp" ,但是它将删除c:\ temp \ 123

I tried with pushd "c:\temp\123" && rd /s /q "c:\temp" but it deletes all subfolders and files in c:\temp\123

任何人都可以帮忙吗?

推荐答案

您可以通过以下方式进行操作:

You could do it the following way:

pushd "C:\Temp" || exit /B 1
for /D %%D in ("*") do (
    if /I not "%%~nxD"=="123" rd /S /Q "%%~D"
)
for %%F in ("*") do (
    del "%%~F"
)
popd

这与这种方法非常相似:批处理命令可删除文件夹中除一个文件之外的所有内容(子文件夹和文件)

This is very similar to this approach: Batch command to delete everything (sub folders and files) from a folder except one file.

这篇关于删除除一个以外的所有文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆