Remove Subversion .svn Files In Folders

By Robbe Morris

.NET code sample to delete all .svn files in a folder hierarchy.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Text;
using System.IO;
using System.Security.AccessControl;

namespace SVNFolderCleanUp
{
    class Program
    {
      static void Main(string[] args)
      {
             // Change the folder or set it to use args[0];

         var folders = Directory.GetDirectories(@"C:\temp");

         try
         {
             foreach (string folder in folders)
             {
                 RemoveFolders(folder);
             }
         }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message + " " + ex.StackTrace);
        }
      }

      public static void RemoveFolders(string folder)
      {
         var directory = new DirectoryInfo(folder);

         if (directory.Name == ".svn")
         {
            RemoveFiles(directory);
            Directory.Delete(directory.FullName,true);
            return;
          }

          var folders = Directory.GetDirectories(directory.FullName);

          if (folders == null) return;

          foreach (string f in folders)
          {
            RemoveFolders(f);
          }

      }

      private static void RemoveFiles(DirectoryInfo directory)
      {
         var files = directory.GetFiles();

         foreach (FileInfo file in files)
         {
            file.Attributes = FileAttributes.Normal;
            File.Delete(file.FullName);
          }

         // Let operating system catch up and prevent
        // "directory is not empty" errors.

        System.Threading.Thread.Sleep(10);

         var folders = Directory.GetDirectories(directory.FullName);

         if (folders == null || folders.Length == 0)
         {
             System.Threading.Thread.Sleep(10);
             Directory.Delete(directory.FullName);
             return;
         }

         foreach (string f in folders)
         {
           RemoveFiles(new DirectoryInfo(f));
         }

      }

   }
}

Remove Subversion .svn Files In Folders  (832 Views)