groovy
  1. groovy
  2. GROOVY-1605

Have a delete() method in File that deletes all subdirectories and contained files

    Details

    • Type: New Feature New Feature
    • Status: Closed Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.1-beta-2
    • Fix Version/s: 1.6-rc-1, 1.5.8, 1.7-beta-1
    • Component/s: groovy-jdk
    • Labels:
      None
    • Environment:
      all
    • Number of attachments :
      2

      Description

      Deleting a directory including all subdirectories is a very tedious task. A method in File like "deleteRecursively()" would really help.

      I am currently using already qutie nice code like this:

      def dataDir = new File( ... path ...)

      def dirs = []

      dataDir.eachFileRecurse {
      if (!it.isDirectory()) log.info("Deleting $

      {it.name}: ${it.delete()}")
      else dirs << it
      }

      dirs.reverse().each { log.info("Deleting directory: ${it.name}

      : $

      {it.delete()}

      ") }

      But why could it not be new File ( ... dir ...).deleteDir() or similar?

      1. DefaultGroovyMethods-File.deleteDir.txt
        2 kB
        Joachim Heldmann
      2. DirectoryDeleteTest.groovy
        1 kB
        Joachim Heldmann

        Activity

        Hide
        Paul King added a comment -

        wouldn't this do:

        new File("...path...").eachFileRecurse{ it.deleteOnExit() }
        
        Show
        Paul King added a comment - wouldn't this do: new File( "...path..." ).eachFileRecurse{ it.deleteOnExit() }
        Hide
        Thorsten Kamann added a comment -

        deleteOnExit() deletes at shutdown of the JVM. This is often too late.

        I would prefer an additional method like Sven proposed.

        Show
        Thorsten Kamann added a comment - deleteOnExit() deletes at shutdown of the JVM. This is often too late. I would prefer an additional method like Sven proposed.
        Hide
        Joachim Heldmann added a comment -

        I attached a patch with a new Method deleteDir() for File and a test class for this new method.
        Here is the JavaDoc for the method:

        Deletes a directory with all contained files and subdirectories.
        The method returns
        true, when deletion was successful
        true, when it is called for a non existing directory
        false, when it is called for a file which isn't a directory
        false, when directory couldn't be deleted

        Show
        Joachim Heldmann added a comment - I attached a patch with a new Method deleteDir() for File and a test class for this new method. Here is the JavaDoc for the method: Deletes a directory with all contained files and subdirectories. The method returns true, when deletion was successful true, when it is called for a non existing directory false, when it is called for a file which isn't a directory false, when directory couldn't be deleted
        Hide
        Guillaume Laforge added a comment -

        Thanks for the patch!

        Show
        Guillaume Laforge added a comment - Thanks for the patch!
        Hide
        Jim White added a comment -

        How about changing this to File.delete(boolean recurse = false)?

        Show
        Jim White added a comment - How about changing this to File.delete(boolean recurse = false)?

          People

          • Assignee:
            Guillaume Laforge
            Reporter:
            Sven Haiges
          • Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: