After a certain time interval(month) I want to delete the subfolders with content. Can someone help me to achieve this. The following code shows errors related to Iterator.
// Create a ref for closure
def dir
def yesterday = ( new Date() ).time - 1000*60*60*24
//definition Closure
dir = {
while(it.hasNext()){
it.eachDirRecurse( dir )
println("Dir: " + it.canonicalPath)
if(it.lastModified() <= yesterday)
it.deleteDir()
}
}
// Apply closure
dir( new File("H:\\soapUI\\Adres\\") )
This is the Exception:
Caught: groovy.lang.MissingMethodException: No signature of method: java.io.File.hasNext() is applicable for argument types: () values: []
Possible solutions: inspect(), getText(), getText(java.lang.String), setText(java.lang.String), setText(java.lang.String, java.lang.String), hashCode()\
at test$_run_closure1.doCall(test.groovy:8)
at test.run(test.groovy:19)
There’s at least couple of errors in your code…
it.hasNext()on a Fileit.eachDirRecursewhich will recurse down the whole tree, but you then call it again for every directory in that tree…You are also going to have problems as you will remove a directory, but then
eachDirRecursewill still try to walk down into that directory and throw aFileNotFoundExceptionI think you’re going to have to not use
eachDirRecurseAssuming you are on Groovy 1.8 (you don’t say), you can do something like this:
Here’s the documentation for TimeCategory, FileType and File.traverse()