Simple Groovy script to remove .svn directories

The other day I needed to remove a mess of .svn directories from a project as I moved it from my personal box (using svn as the soure control) to the client’s source control system. This is a fairly frequent task, so attempting to be a “productive programmer” I decided that I would take the time to write a script to do this for me. After testing, it took about an hour to write… mainly because I tried to be too fancy, and tried a few things in Groovy I had not seen before. Overall, it was an hour well spent:

  1. search_for(“.”)
  2.  
  3. def search_for(dir) {
  4.         homedir = new File(dir)
  5.         homedir.eachDir {
  6.                 if (it.name == “.svn”) {
  7.                         println it.canonicalPath
  8.                         deleteDirectory(it)
  9.                 }
  10.                 else {
  11.                         search_for(it.canonicalPath)
  12.                 }
  13.         }
  14. }
  15.  
  16. def deleteDirectory(dir) {
  17.         dir.eachDir { deleteDirectory(it) }
  18.         dir.eachFile { it.delete() }
  19.         dir.delete()
  20. }

14 lines. I really like the eachDir and the eachFile methods on the File object… and there is even an eachFileRecurse. All I can say is: where the heck is the eachDirRecurse??? Oh well. It was still a good learning experience, and now I have a malleable tool next time I run into something similar.