JBDenver
JBDenver

Reputation: 29

Find a file in the directories that have been modified in the last hour

I have a very large file system with a folder structure that can be 4 levels deep. I have a file (i.e. test.log) that can be anywhere in this folder structure. Rather than traversing through the entire folder structure recursively, I want to apply some selection and only search folders that have been modified in the last 4 hours. How would I use the Groovy traverse to perform an operation like that? Thanks

Upvotes: 2

Views: 232

Answers (1)

Opal
Opal

Reputation: 84864

Maybe this will work (have no possibility to check):

import static groovy.io.FileType.*
import static groovy.io.FileVisitResult.*
import groovy.time.TimeCategory

currentDate =  new Date()
use( TimeCategory ) {
    before4hours = currentDate - 4.hours
}

def toTravserse = new File('<SOME_DIR>')
toTravserse.traverse(
        type         : FILES,
        maxDepth     : 4,
        nameFilter   : ~/test.log/,
        preDir       : { if (it.lastModified() < before4hours.time) return SKIP_SUBTREE },
) { println "Found: $it" }

Have a look at the docs.

Upvotes: 1

Related Questions