如何使用 PowerShell 递归删除具有特定名称的文件夹?

发布于 2024-09-17 06:31:40 字数 206 浏览 3 评论 0原文

我可以使用以下命令删除多个文件夹中具有特定扩展名的文件:

Get-childitem * -include *.scc -recurse | remove-item

但我还需要删除具有特定名称的文件夹 - 特别是当您从 subversion 存储库中拉取文件时 subversion 创建的文件夹(“.svn”或“_svn”) 。

I can delete files with specific extensions in multiple folders with this:

Get-childitem * -include *.scc -recurse | remove-item

But I also need to delete folders with a specific name - in particular those that subversion creates (".svn" or "_svn") when you pull down files from a subversion repo.

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

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

发布评论

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

评论(3

傲鸠 2024-09-24 06:31:44

我倾向于避免在 Get-ChildItem 上使用 -Ininclude 参数,因为它比 -Filter 慢。但是在这种情况下(因为它不能表示为 -Filter),这就是我将使用的:

Get-ChildItem . -Include .svn,_svn -Recurse -Force | Remove-Item -Recurse -Force

或者如果在提示符下键入:

ls . -inc .svn,_svn -r -fo | ri -r -fo

I tend to avoid the -Include parameter on Get-ChildItem as it is slower than -Filter. However in this instance (since it can't be expressed as a -Filter), this is what I would use:

Get-ChildItem . -Include .svn,_svn -Recurse -Force | Remove-Item -Recurse -Force

or if typing this at the prompt:

ls . -inc .svn,_svn -r -fo | ri -r -fo
为你拒绝所有暧昧 2024-09-24 06:31:44

为了快速删除,使用 -Filter

递归删除具有特定名称的文件夹 -Filter 明显比 -Ininclude 快。

因此,为了可见性,这里是 @Keith 答案的更快版本:

完全输入:

Get-ChildItem -Filter .svn -Recurse -Force | Remove-Item -Recurse -Force

简短版本:

ls . -filter .svn -r -fo | ri -r -fo

For fast deletion, use -Filter

To recursively delete a folder with a specific name -Filter is significantly faster than -Include.

So, for visibility, here is the faster version of @Keith's answer:

Fully typed-out:

Get-ChildItem -Filter .svn -Recurse -Force | Remove-Item -Recurse -Force

Short version:

ls . -filter .svn -r -fo | ri -r -fo
此刻的回忆 2024-09-24 06:31:43

这个一个应该做到这一点:

get-childitem -Include .svn -Recurse -force | Remove-Item -Force -Recurse

其他版本:

$fso = New-Object -com "Scripting.FileSystemObject"
$folder = $fso.GetFolder("C:\Test\")

foreach ($subfolder in $folder.SubFolders)
{
    If ($subfolder.Name -like "*.svn")
    {
        remove-item $subfolder.Path -Verbose
    }       
}

This one should do it:

get-childitem -Include .svn -Recurse -force | Remove-Item -Force -Recurse

Other version:

$fso = New-Object -com "Scripting.FileSystemObject"
$folder = $fso.GetFolder("C:\Test\")

foreach ($subfolder in $folder.SubFolders)
{
    If ($subfolder.Name -like "*.svn")
    {
        remove-item $subfolder.Path -Verbose
    }       
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文