Lista recursiva de todos los archivos que coinciden con un cierto tipo de archivo en Groovy
Estoy tratando de listar recursivamente todos los archivos que coinciden con un tipo de archivo en particular en Groovy. Este ejemplo casi lo hace. Sin embargo, no lista los archivos en la carpeta raíz. ¿Hay alguna forma de modificar esto para listar archivos en la carpeta raíz? O, ¿hay una manera diferente de hacerlo?
4 answers
Esto debería resolver tu problema:
import static groovy.io.FileType.FILES
new File('.').eachFileRecurse(FILES) {
if(it.name.endsWith('.groovy')) {
println it
}
}
eachFileRecurse
toma un tipo de archivo enum que especifica que solo le interesan los archivos. El resto del problema se resuelve fácilmente filtrando el nombre del archivo. Vale la pena mencionar que eachFileRecurse
normalmente se repite sobre ambos archivos y carpetas, mientras que eachDirRecurse
solo encuentra carpetas.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-08 07:37:02
Groovy versión 2.4.7:
new File(pathToFolder).traverse(type: groovy.io.FileType.FILES) { it ->
println it
}
También puede agregar un filtro como
new File(parentPath).traverse(type: groovy.io.FileType.FILES, nameFilter: ~/patternRegex/) { it ->
println it
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-12-29 17:42:46
Sustitúyase eachDirRecurse
por eachFileRecurse
y debería funcionar.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-07 20:09:43
// Define closure
def result
findTxtFileClos = {
it.eachDir(findTxtFileClos);
it.eachFileMatch(~/.*.txt/) {file ->
result += "${file.absolutePath}\n"
}
}
// Apply closure
findTxtFileClos(new File("."))
println result
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-08 12:19:59