Question

I am working on converting a KornShell (ksh) script to Groovy. I have the following Find command - what would be a Groovy way to do something similar, without relying on Unix commands (I need this to work cross-platform, so I can not do a "blah blah".execute()).

find <source directory> -name <file pattern> -type f -mtime +140 -level 0

This code searches for all files in the source directory (no subdirs) that match a file pattern and are older than 140 days.

Was it helpful?

Solution

Groovy provides some methods for searching through directories: File.eachFile for the -level 0 case, or File.eachFileRecurse for the general case. Example:

use(groovy.time.TimeCategory) {
    new File(".").eachFile { file ->
        if (file.isFile() &&
            file.lastModified() < (new Date() - 140.days).time) {
            println file
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top