tags:

views:

28

answers:

1

Newbie question:

I'm trying to get (not print , that's easy) the list of files in a directory and it's sub directories. I've tried:

def folder = "C:\\DevEnv\\Projects\\Generic";
def baseDir = new File(folder);
files = baseDir.listFiles();

but I only get the dirs. I've also tried

def files = [];     

def processFileClosure = { 
        println "working on ${it.canonicalPath}: " 
        files.add (it.canonicalPath);                
    }   

baseDir.eachFileRecurse(FileType.FILES, processFileClosure);

but "files" is not recognized in the scope of the closure.

How do I get the list?

+1  A: 

This code works for me:

import groovy.io.FileType

def list = []

def dir = new File("path_to_parent_dir")
dir.eachFileRecurse (FileType.FILES) { file ->
  list << file
}

Afterwards the list variable contains all files (java.io.File) of the given directory and its subdirectories:

list.each {
  println it.path
}
Christoph Metzendorf