Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 repo.

like image 888
Tone Avatar asked Sep 05 '10 22:09

Tone


People also ask

How do I delete a directory in PowerShell recursively?

In the PowerShell console, type Remove-Item –path c:\testfolder –recurse and press Enter, replacing c:\testfolder with the full path to the folder you want to delete.

How do you recursively delete a file in PowerShell?

Using PowerShell to Delete All Files Recursively If you need to also delete the files inside every sub-directory, you need to add the -Recurse switch to the Get-ChildItem cmdlet to get all files recursively.

How do I delete a folder content in PowerShell?

Use the Delete() Method Every object in PowerShell has a Delete() method and you can use it to remove that object. To delete files and folders, use the Get-ChildItem command and use the Delete() method on the output.

How do I delete a recursive folder?

To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.


2 Answers

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     }        } 
like image 64
Leniel Maccaferri Avatar answered Oct 10 '22 08:10

Leniel Maccaferri


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 
like image 20
Keith Hill Avatar answered Oct 10 '22 09:10

Keith Hill