5

我正在将 KornShell (ksh) 脚本转换为 Groovy。我有以下 Find 命令 - 在不依赖 Unix 命令的情况下,做类似事情的 Groovy 方法是什么(我需要它来跨平台工作,所以我不能执行“blah blah”.execute())。

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

此代码搜索源目录(无子目录)中与文件模式匹配且超过 140 天的所有文件。

4

1 回答 1

6

Groovy 提供了一些在目录中搜索的方法:File.eachFile针对-level 0案例,或者File.eachFileRecurse针对一般案例。例子:

use(groovy.time.TimeCategory) {
    new File(".").eachFile { file ->
        if (file.isFile() &&
            file.lastModified() < (new Date() - 140.days).time) {
            println file
        }
    }
}
于 2012-12-21T20:30:06.713 回答