Wednesday, January 25, 2012

File/Directory Traverse

//helpful file properties: http://groovy.codehaus.org/JN2015-Files
 
import groovy.io.FileType

def walkFiles = {filepath, filterOnly, onFind, onEnd = {} ->
    try {
        File f = new File(filepath)
        f.traverse([type:FileType.FILES, nameFilter:filterOnly], onFind)
        onEnd()
    }
    catch (FileNotFoundException e) { println "ERROR: invalid file/directory"}
}

def pf = { file ->
    if (file.name.contains("a"))
        println file.name
}

walkFiles("C:\\123", ~/.*\.ico/, pf)

No comments:

Post a Comment