如何使用 Windows 命令提示符删除空文件夹?
我需要使用 Windows 命令提示符从应用程序文件夹中删除所有空文件夹?
我怎样才能创建这样的bat文件?
请帮我。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我需要使用 Windows 命令提示符从应用程序文件夹中删除所有空文件夹?
我怎样才能创建这样的bat文件?
请帮我。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(17)
您可以使用
ROBOCOPY
命令。它非常简单,也可用于删除大型层次结构中的空文件夹。这里源和目标都是
folder1
,因为您只需要删除空文件夹,而不是将其他(必需)文件移动到不同的文件夹。/S
选项是跳过复制(移动 - 在上述情况下)空文件夹。由于文件在同一驱动器内移动,因此速度也更快。You can use the
ROBOCOPY
command. It is very simple and can also be used to delete empty folders inside large hierarchy.Here both source and destination are
folder1
, as you only need to delete empty folders, instead of moving other(required) files to different folder./S
option is to skip copying(moving - in the above case) empty folders. It is also faster as the files are moved inside the same drive.一种更简单的方法是使用 /s 开关执行 xcopy 来复制整个目录结构。 /s 的帮助说复制目录和子目录(空目录除外)。
其中 dirA 是带有空文件夹的源。 DirB 将是没有空文件夹的副本
A simpler way is to do xcopy to make a copy of the entire directory structure using /s switch. help for /s says Copies directories and subdirectories except empty ones.
where dirA is source with Empty folders. DirB will be the copy without empty folders
来自: http://blogs.msdn.com/b /oldnewthing/archive/2008/04/17/8399914.aspx
当然,在执行该命令之前,我会先测试它而不删除。另外,这里有一个来自评论的修改版本,其中包括带有空格的文件夹:
PS,博客文章中还有更多评论可能会对您有所帮助,因此在尝试之前也请务必阅读这些评论
from: http://blogs.msdn.com/b/oldnewthing/archive/2008/04/17/8399914.aspx
Of course I'd test it first without deleting before I do that command. Also, here's a modded version from the comments that includes folders with spaces:
P.S. there are more comments in the blog post that might help you out so be sure to read those too before you try this out
你不需要 usebackq:
You don't need usebackq:
从同一引用页面添加到被腐蚀的答案是 PowerShell 版本 http://blogs.msdn.com/b/oldnewthing/archive/2008/ 04/17/8399914.aspx#8408736
Get-ChildItem -Recurse 。 |其中 { $_.PSISContainer -and @( $_ | Get-ChildItem ).Count -eq 0 } | Remove-Item
或者更简洁地说,
gci -R 。 |其中 { $_.PSISContainer -and @( $_ | gci ).Count -eq 0 } | ri
归功于发布者
Adding to corroded answer from the same referenced page is a PowerShell version http://blogs.msdn.com/b/oldnewthing/archive/2008/04/17/8399914.aspx#8408736
Get-ChildItem -Recurse . | where { $_.PSISContainer -and @( $_ | Get-ChildItem ).Count -eq 0 } | Remove-Item
or, more tersely,
gci -R . | where { $_.PSISContainer -and @( $_ | gci ).Count -eq 0 } | ri
credit goes to the posting author
从命令行:
for /R /D %1 in (*) do rd "%1"
在批处理文件中
for /R /D %%1 in (*) do rd "%%1"
我不知道是否有这样的记录,但它适用于 W2K、XP 和 Win 7。而且我不知道是否如此总是有效,但它永远不会意外删除文件。
from the command line:
for /R /D %1 in (*) do rd "%1"
in a batch file
for /R /D %%1 in (*) do rd "%%1"
I don't know if it's documented as such, but it works in W2K, XP, and Win 7. And I don't know if it will always work, but it won't ever delete files by accident.
这是上述的混合体。它会删除超过 X 天的所有文件,并删除给定路径的所有空文件夹。使用时只需设置日期、文件夹路径和驱动器
This is a hybird of the above. It removes ALL files older than X days and removes any empty folders for the given path. To use simply set the days, folderpath and drive
它会工作得很好。这是删除旧文件并递归删除空目录的最佳方法。
下面的 .bat 文件是,
占位符需要替换如下(不带引号):
该脚本已在 Windows 7 和 Windows Server 2003 下成功测试。
It will be worked fine. This is best way to delete old files and remove empty directories recursively.
following .bat file is,
The placeholders needs to be replaced as follows (without the quotation marks):
The script has been successfully tested under Windows 7 and Windows Server 2003.
安装适用于 Windows 的任何 UNIX 解释器(Cygwin 或 Git Bash)并运行 cmd:
(并没有真正使用 Windows cmd 提示符,但它很简单,只需几秒钟即可运行)
Install any UNIX interpreter for windows (Cygwin or Git Bash) and run the cmd:
(not really using the windows cmd prompt but it's easy and took few seconds to run)
一个更现代、更简单的解决方案是:
这将尝试在位于
C:\Path\To\Folder
中的所有目录上执行rd
。包含内容的目录不会被删除。如果排除
/p C:\Path\To\Folder
那么它将在当前目录中运行。如果您添加
/s
(在/c
之前),那么它也会查找子目录。A more modern and easier solution is:
This will attempt to execute
rd
on all the directories located inC:\Path\To\Folder
. Directories that have content in them will not be deleted.If you exclude
/p C:\Path\To\Folder
then it'll run in the current directory.If you add
/s
(before the/c
) then it'll also look in sub-directories.如果您想在资源管理器上下文菜单(即右键单击)中使用 Varun 的 ROBOCOPY 命令行,这里是 Windows 注册表导入。我尝试将此作为评论添加到他的答案中,但内联标记不可行。
我已在我自己的 Windows 10 PC 上对此进行了测试,但使用风险需您自担。它将打开一个新的命令提示符,运行命令,然后暂停,以便您可以看到输出。
复制到新的文本文件中:
Windows 注册表编辑器版本 5.00
[HKEY_CURRENT_USER\Software\Classes\directory\Background\shell\删除空文件夹\命令]
@="C:\Windows\System32\Cmd.exe /C \"C:\Windows\System32\Robocopy.exe \"%V\" \"%V\" /s /move\" && 暂停”
[HKEY_CURRENT_USER\Software\Classes\directory\shell\删除空文件夹\命令]
@="C:\Windows\System32\Cmd.exe /C \"C:\Windows\System32\Robocopy.exe \"%V\" \"%V\" /s /move\" && 暂停"
将 .txt 扩展名重命名为 .reg
If you want to use Varun's ROBOCOPY command line in the Explorer context menu (i.e. right-click) here is a Windows registry import. I tried adding this as a comment to his answer, but the inline markup wasn't feasible.
I've tested this on my own Windows 10 PC, but use at your own risk. It will open a new command prompt, run the command, and pause so you can see the output.
Copy into a new text file:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Classes\directory\Background\shell\Delete Empty Folders\command]
@="C:\Windows\System32\Cmd.exe /C \"C:\Windows\System32\Robocopy.exe \"%V\" \"%V\" /s /move\" && PAUSE"
[HKEY_CURRENT_USER\Software\Classes\directory\shell\Delete Empty Folders\command]
@="C:\Windows\System32\Cmd.exe /C \"C:\Windows\System32\Robocopy.exe \"%V\" \"%V\" /s /move\" && PAUSE"
Rename the .txt extension to .reg
D:\Fun 是包含空文件夹的文件夹
文件夹名称中存在空格时使用双引号(本例中不需要)
D:\Fun is the folder that contains empty folders
double quotation in the case of space in folder name (no need in this example)
以下 .cmd 是一个实验(有效):
%temp% = C:\Users(用户)\AppData\Local\Temp | %userprofile%\AppData\Local\Temp
代码:
注意:
作为“预处理”的一部分,进行此实验的触发因素是为了防止(也已过时的)声卡的特定(过时的)驱动程序在每次启动时自动重新安装。以及干净的(幻影的)隐藏设备等等。 (...)(这篇笔记只是想了解一下使用的想法)
所以,简而言之:
这是清理留下的“temp”垃圾并提供报告的部分。
The following .cmd is an experiment (that works) to:
%temp% = C:\Users(user)\AppData\Local\Temp | %userprofile%\AppData\Local\Temp
code:
Note that:
The trigger to Experiment with this, as part of a 'pre-process', was to prevent the specific (obsolete) driver of the (also obsolete) sound card from automatically reinstalling at each boot. And clean (ghosted) hidden Devices and more other. (...) (this note was just about to get an idea for the use)
So, in a few words:
This is the part that cleans up the 'temp' garbage left behind and gives a report.
在forfiles中使用for:
自下而上删除需要排序/R
Use for in forfiles:
sort /R required to delete bottom-up
好吧,只是一个快速而肮脏的建议,针对没有空格的简单一级目录结构,[编辑]以及仅包含我发现有用的一种类型文件的目录(在某些时候来自 http://www.pcreview.co.uk/forums/can-check-if-folder-empty-bat-file-t1468868.html):
/ad : 仅显示目录条目
/b :使用裸格式(仅名称)
[编辑] 使用普通星号检查任何文件(上面的%a\*)将不起作用,感谢您的更正
,因此删除将是:
well, just a quick and dirty suggestion for simple 1-level directory structure without spaces, [edit] and for directories containing only ONE type of files that I found useful (at some point from http://www.pcreview.co.uk/forums/can-check-if-folder-empty-bat-file-t1468868.html):
/ad : shows only directory entries
/b : use bare format (just names)
[edit] using plain asterisk to check for ANY file (%a\* above) won't work, thanks for correction
therefore, deleting would be:
这可以通过使用带有两个参数的 rd 命令轻松完成:
/Q - 安静模式,不询问是否可以删除目录树
/S
/S - 删除指定目录中的所有目录和文件
除了目录本身。用于删除目录树。
This can be easily done by using
rd
command with two parameters:/Q - Quiet mode, do not ask if ok to remove a directory tree with
/S
/S - Removes all directories and files in the specified directory
in addition to the directory itself. Used to remove a directory tree.