PowerShell:查找并删除其中或子文件夹中没有文件的文件夹
我有一个 PowerShell 2.0 脚本,用于删除其中没有文件的文件夹:
dir 'P:\path\to\wherever' -recurse | Where-Object { $_.PSIsContainer } | Where-Object { $_.GetFiles().Count -eq 0 } | foreach-object { remove-item $_.fullname -recurse}
但是,我注意到运行该脚本时出现大量错误。即:
Remove-Item : Directory P:\path\to\wherever cannot be removed because it is not empty.
“什么?!”我惊慌失措。它们应该全部都是空的!我只过滤空文件夹!显然这并不是脚本的工作方式。在这种情况下,仅包含文件夹作为子级但文件作为孙级的文件夹将被视为空文件:
Folder1 (no files - 1 folder) \ Folder 2 (one file)
在这种情况下,PowerShell 会将Folder1 视为空并尝试将其删除。这让我困惑的原因是,如果我在 Windows 资源管理器中右键单击“Folder1”,它会显示“Folder1”内有 1 个文件夹和 1 个文件。无论使用什么方式从资源管理器中计算Folder1下的子对象,都允许它无限地查看孙对象。
问题:
如果我的脚本包含孙级或更高级别的文件,如何使我的脚本不将其视为空文件夹?
I have a PowerShell 2.0 script that I use to delete folders that have no files in them:
dir 'P:\path\to\wherever' -recurse | Where-Object { $_.PSIsContainer } | Where-Object { $_.GetFiles().Count -eq 0 } | foreach-object { remove-item $_.fullname -recurse}
However, I noticed that there were a ton of errors when running the script. Namely:
Remove-Item : Directory P:\path\to\wherever cannot be removed because it is not empty.
"WHAT?!" I panicked. They should all be empty! I filter for only empty folders! Apparently that's not quite how the script is working. In this scenario a folder that has only folders as children, but files as grandchildren is considered empty of files:
Folder1 (no files - 1 folder) \ Folder 2 (one file)
In that case, PowerShell sees Folder1 as being empty and tries to delete it. The reason this puzzles me is because if I right-click on Folder1 in Windows Explorer It says that Folder1 has 1 folder and 1 file within it. Whatever is used to calculate the child objects underneath Folder1 from within Explorer allows it to see grandchild objects ad infinitum.
Question:
How can I make my script not consider a folder empty if it has files as grandchildren or beyond?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是我在最近的脚本中使用的递归函数......
Here's a recursive function I used in a recent script...
更新递归删除:
您可以使用如下所示的嵌套管道:(
从此处 - 如何使用 PowerShell 删除空子文件夹?)
添加
($_.GetDirectories().Count -eq 0)
条件:不过,这里有一种更简洁的方法:
请注意,在删除项目时不需要
Foreach-Object
。还要向Remove-Item
添加一个-whatif
以查看它是否会执行您期望的操作。Updating for recursive deletion:
You can use a nested pipeline like below:
(from here - How to delete empty subfolders with PowerShell?)
Add a
($_.GetDirectories().Count -eq 0)
condition too:Here is a more succinct way of doing this though:
Note that you do not need the
Foreach-Object
while doing remove item. Also add a-whatif
to theRemove-Item
to see if it is going to do what you expect it to.制作此脚本时遇到了一些问题,其中之一是使用它来检查文件夹是否为空:
但是,我发现空文件夹的大小不是 0 而是 NULL。以下是我将使用的 PowerShell 脚本。它不是我自己的。相反,它来自 PowerShell MVP Richard Siddaway。您可以在 PowerShell.com 上的此线程。
There were some issues in making this script, one of them being using this to check if a folder is empty:
However, I discovered that empty folders are not sized with 0 but rather NULL. The following is the PowerShell script that I will be using. It is not my own. Rather, it is from PowerShell MVP Richard Siddaway. You can see the thread that this function comes from over at this thread on PowerShell.com.
您可以为此使用递归函数。其实我已经写过一篇了:
You can use a recursive function for this. I actually have already written one: