Forum Discussion

harry21's avatar
harry21
Occasional Contributor
8 years ago

Groovy to delete all the files in a directory

Can someone direct me how to delete all the files in a directory.. (As a prerequisite, this needs to be done before running each testcase)

I've googled it and couldn't get the relevant answer. I tried with following snippet

 

def del = FileUtils.cleanDirectory(filePath) 

 

 

But this is not working! Getting MissingPropertyException message!

  • You are missing a couple of points here.

    1. You need to import the FileUtils class to use it.
    2. You cleanDirectory method accepts File data type, and you seem to try and pass a string.

    Here is a snippet that should work:

    import org.apache.commons.io.FileUtils
    File folder = new File("C:/Work/composite")
    FileUtils.cleanDirectory(folder)

    There probably is a shorter way, but this seems clean enough :)

  • You are missing a couple of points here.

    1. You need to import the FileUtils class to use it.
    2. You cleanDirectory method accepts File data type, and you seem to try and pass a string.

    Here is a snippet that should work:

    import org.apache.commons.io.FileUtils
    File folder = new File("C:/Work/composite")
    FileUtils.cleanDirectory(folder)

    There probably is a shorter way, but this seems clean enough :)