How can I recursively delete folder with a specific name with PowerShell?
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 r开发者_C百科epo.
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
}
}
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
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
精彩评论