Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a list of all the files in a directory (recursive)

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

I've tried:

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

I only get the directories. 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?

like image 919
Yossale Avatar asked Oct 17 '10 15:10

Yossale


People also ask

How do I recursively list files in Windows?

How to list all files a folder recursively in command line ? With a ms dos prompt, to list all the files inside a folder. To do so, type the dir command to list the folder content with the recursive dedicated option.

How do I list files in a directory and subdirectories?

The ls command is used to list files or directories in Linux and other Unix-based operating systems. Just like you navigate in your File explorer or Finder with a GUI, the ls command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.

What is recursive directory listing?

Recursively lists all files and directories in the current directory and any subdirectories, in wide format, pausing after each screen of output.


1 Answers

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 } 
like image 82
Christoph Metzendorf Avatar answered Sep 29 '22 20:09

Christoph Metzendorf