Remove files recursively..
// March 10th, 2009 // No Comments » // Source Control, powershell
I had a problem a couple of weeks ago, when my main Subversion repository completely died out of data corruption. I was trying to check-in my latest changes, but I just couldn’t. Nothing was lost since I do regular backups of my repositories. So I was in good shape. I thought to myself this was the best chance to migrate to TFS, which I have been wanting to do for a while.
Note: I did not leave subversion in favor of TFS because of this corruption. This data corruption was expected to happen because the machine usually gets killed in the middle of backup or by the occasional “Windows Update”; so the repository was destined to die eventually. I could have fixed the repository in no time with the tools that Subversion provides or simply by restoring backups. But I moved to TFS mainly because of the project integration in all aspects and not only source control, and because most of my clients use TFS, so integration with their systems is also easier.
Anyways I needed a clean copy of my newest changes so that I could import it into TFS. I felt lazy at the moment and I chose the complex solution, clean up my subversion repository copy. My project has a LOT of folders, so deleting the .svn folders became a HUGE effort. I decided to use a simple shell command to do that, like I just to do in linux:
rm -rf `find . -type d -name .svn`
So I started looking on google to see what was the way to it in Powershell and I found really complex solutions like:
$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
}
}
Source: Here
It really scared me! I thought that, if that was the easiest way to delete recursively, then powershell is doomed. Thanks to google, I did find a simpler way to do this:
dir . -recurse .svn -fo | remove-item -force
Which is quite elegant and fast! Sometimes I just don’t understand why it is so difficult to come up with the simple answer. It happens to me quite often. It is extremely easy to come up with a complex solution…but to actually have a simple solution is always difficult. This is only one example.
Anyways this did the trick and it works perfectly.
[Update]: I forgot to add the ‘-fo’ option which shows the hidden files (in this case the .svn folders). Otherwise this would not work.
