如何使用 Windows 命令提示符删除空文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7831286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:50:29  来源:igfitidea点击:

How to delete empty folders using windows command prompt?

windowscommand-linecommanddirectory

提问by DEVOPS

I need to delete all empty folders from my application folder using windows command prompt?

我需要使用 Windows 命令提示符从我的应用程序文件夹中删除所有空文件夹吗?

How can I create a bat file like that?

我怎样才能创建一个这样的bat文件?

Please help me.

请帮我。

采纳答案by corroded

for /f "usebackq" %%d in (`"dir /ad/b/s | sort /R"`) do rd "%%d"

from: http://blogs.msdn.com/b/oldnewthing/archive/2008/04/17/8399914.aspx

来自: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:

当然,在执行该命令之前,我会先对其进行测试而不删除。此外,这是评论中的修改版本,其中包含带空格的文件夹:

 for /f "usebackq delims=" %%d in (`"dir /ad/b/s | sort /R"`) do rd "%%d"

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

PS,博客文章中有更多评论可能会对您有所帮助,因此请务必在尝试之前阅读这些评论

回答by Varun Sharma

You can use ROBOCOPY. It is very simple and can also be used to delete empty folders inside large hierarchy.

您可以使用 ROBOCOPY。它非常简单,也可用于删除大型层次结构中的空文件夹。

ROBOCOPY folder1 folder1 /S /MOVE

Here both source and destination are folder1, as you only need to delete empty folders, instead of moving other(required) files to different folder. /Soption is to skip copying(moving - in the above case) empty folders. It is also faster as the files are moved inside the same drive.

这里源和目标都是folder1,因为您只需要删除空文件夹,而不是将其他(必需的)文件移动到不同的文件夹。/S选项是跳过复制(移动 - 在上述情况下)空文件夹。由于文件在同一驱动器内移动,因此速度也更快。

回答by Dinesh Rajan

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.

更简单的方法是执行 xcopy 以使用 /s 开关制作整个目录结构的副本。/s 的帮助说复制目录和子目录,除了空目录。

xcopy dirA dirB /S

where dirA is source with Empty folders. DirB will be the copy without empty folders

其中 dirA 是带有空文件夹的源。DirB 将是没有空文件夹的副本

回答by tomasz86

You don't need usebackq:

你不需要 usebackq:

FOR /F delims^= %%A IN ('DIR/AD/B/S^|SORT/R') DO RD "%%A"

回答by BozoJoe

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

从同一引用页面添加到腐蚀的答案是 PowerShell 版本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

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

gci -R . | where { $_.PSISContainer -and @( $_ | gci ).Count -eq 0 } | ri

credit goes to the posting author

归功于发帖作者

回答by jcwhall

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

这是上述的混合体。它删除所有早于 X 天的文件,并删除给定路径的任何空文件夹。使用只需设置日期、文件夹路径和驱动器

@echo off
SETLOCAL
set days=30
set folderpath=E:\TEST\
set drive=E:

::Delete files
forfiles -p %folderpath% -s -d -%days% -c "cmd /c del /q @path "

::Delete folders
cd %folderpath%
%drive%
for /f "usebackq delims=" %%d in (`"dir /ad/b/s | sort /R"`) do rd "%%d"`

回答by Oglethorpe Cadwallader

from the command line: for /R /D %1 in (*) do rd "%1"

从命令行: for /R /D %1 in (*) do rd "%1"

in a batch file for /R /D %%1 in (*) do rd "%%1"

在 (*) 中 /R /D %%1 的批处理文件中执行 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.

我不知道它是否是这样记录的,但它可以在 W2K、XP 和 Win 7 中运行。我不知道它是否会一直运行,但它永远不会意外删除文件。

回答by GreAce

Install any UNIX interpreter for windows (Cygwin or Git Bash) and run the cmd:

安装任何适用于 Windows 的 UNIX 解释器(Cygwin 或 Git Bash)并运行 cmd:

find /path/to/directory -empty -type d

find /path/to/directory -empty -type d

To find them

find /path/to/directory -empty -type d -delete

find /path/to/directory -empty -type d -delete

To delete them

(not really using the windows cmd prompt but it's easy and took few seconds to run)

(不是真正使用 windows cmd 提示符,但它很容易运行并且需要几秒钟的时间)

回答by sathish anish

It will be worked fine. This is best way to delete old files and remove empty directories recursively. following .bat file is,

它会工作得很好。这是删除旧文件和递归删除空目录的最佳方法。以下 .bat 文件是,

forfiles /p [PATH] /s /m [FILE-PATTERN] /d -[DAYS] /c "cmd /c del @path"
for /f "delims=" %%d in ('dir [PATH] /s /b /ad ^| sort /r') do rd "%%d"

The placeholders needs to be replaced as follows (without the quotation marks):

占位符需要替换如下(不带引号):

[DAYS] = Max. age of the files in days, e.g. “10”
[PATH] = Path to search for old files and empty folders, e.g. “C:\Backup\”
[FILE-PATTERN] = Pattern that matches files to delete, e.g. “*.bkp”

The script has been successfully tested under Windows 7 and Windows Server 2003.

该脚本已在 Windows 7 和 Windows Server 2003 下成功测试。

回答by Casey Plummer

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.

如果您想在资源管理器上下文菜单(即右键单击)中使用 Varun 的 ROBOCOPY 命令行,这里是 Windows 注册表导入。我尝试将此作为评论添加到他的答案中,但内联标记不可行。

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.

我已经在我自己的 Windows 10 PC 上对此进行了测试,但使用风险自负。它将打开一个新的命令提示符,运行命令,然后暂停以便您可以看到输出。

  1. 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"

  2. Rename the .txt extension to .reg

  3. Double click to import.
  1. 复制到一个新的文本文件中:

    Windows 注册表编辑器 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"

  2. 将 .txt 扩展名重命名为 .reg

  3. 双击导入。