List files from given directory with Recursion

java program to list all files in a directory and nested sub-directories
java list files in directory recursively
java 8 list files recursively
java list subdirectories recursively
java get all files in directory and subdirectories java 8
scala list all files in directory recursively
recursively list all files in directory java
recursive directory search

I'm given a data object like so, where [{...}] is the same type of object

{
 dirName: 'app',
 files: ['index.html', 'page.html'],
 subDirs: [{...}]
}

You could return a combination of directory and files and call the function again for getting nested values.

function getFiles({ dirName, files = [], subDirs }) {
    return files
        .concat(...(subDirs ? subDirs.map(getFiles) : []))
        .map(f => [dirName].concat(f));
}

var data = { dirName: 'app', files: ['index.html'], subDirs: [{ dirName: 'js', files: ['main.js', 'app.js', 'misc.js'], subDirs: [{ dirName: 'vendor', files: ['jquery.js', 'underscore.js'], subDirs: [] }] }, { dirName: 'css', files: ['reset.css', 'main.css',], subDirs: [] }] },
    files = getFiles(data);

console.log(files.map(a => a.join('/')));
console.log(files);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Java program to List all files in a directory and nested sub , Java program to List all files in a directory and nested sub-directories | Recursive approach. Prerequisites : File class. Given a main directory/folder, list all the  Recursive Algorithm : 1. Create File object for main directory. 2. Get array of files for main directory. 3. If array[i] is a file : -> Print out file name. 4. If array[i] is a directory : -> Print out directory name. -> Get array of files for current sub-directory. -> Repeat the step 3 and 4 with current sub-directory. 5.

Try this one, it should list all directories with files in console using cascade tabulation:

function rec(dir, level = 0) {
    level++;
    console.log(`${' '.repeat((level-1)*5)}${dir.dirName}::`);
    console.log(dir.files.map(f => `${' '.repeat(level*5)}${f}`).join('\n'));
    dir.subDirs.forEach(d => rec(d, level));
}

and that one just returns all the files from folder tree:

function rec(dir) {
    return [...dir.files, ...dir.subDirs.flatMap(d => rec(d))];
}

call it like:

rec(fileData)

Hope that helps.

Java File IO - List files and directories recursively, Java code example to to list all files and directories (including sub files a specific directory, however its examples are only for listing the first  If the item is a directory, we have the function recursively call itself to get all of the files and sub-directories inside the given directory. And if the item is a file, we simply append the file path to the arrayOfFiles array. When the forEach loop has finished, we return the arrayOfFiles array. Here is how you use the function in your code:

A great time to learn about mutual recursion. Below, list1 calls listAll, which calls list1 ...

const list1 = ({ dirName, files = [], subDirs = []}, pwd = '') =>
  [ ... files .map (f => `${pwd}/${f}`)
  , ... listAll (subDirs, `${pwd}/${dirName}`)
  ]

const listAll = (dirs = [], pwd = '') =>
  dirs .flatMap (d => list1 (d, pwd))

console .log (list1(fileData))
// [ "/index.html"
// , "/app/main.js"
// , "/app/app.js"
// , "/app/misc.js"
// , "/app/js/jquery.js"
// , "/app/js/underscore.js"
// , "/app/reset.css"
// , "/app/main.css"
// ]

Expand the snippet below to verify the results in your own browser -

const fileData = {
    dirName: 'app',
    files: ['index.html'],
    subDirs: [
      {
        dirName: 'js',
        files: [
          'main.js',
          'app.js',
          'misc.js',
        ],
        subDirs: [
          {
            dirName: 'vendor',
            files: [
              'jquery.js',
              'underscore.js',
            ],
            subDirs: [],
          },
        ],
      },
      {
        dirName: 'css',
        files: [
          'reset.css',
          'main.css',
        ],
        subDirs: [],
      },
    ],
  };
  
const list1 = ({ dirName, files = [], subDirs = []}, pwd = '') =>
  [ ... files .map (f => `${pwd}/${f}`)
  , ... listAll (subDirs, `${pwd}/${dirName}`)
  ]

const listAll = (dirs = [], pwd = '') =>
  dirs .flatMap (d => list1 (d, pwd))
  
console .log (list1 (fileData))
// [ "/index.html"
// , "/app/main.js"
// , "/app/app.js"
// , "/app/misc.js"
// , "/app/js/jquery.js"
// , "/app/js/underscore.js"
// , "/app/reset.css"
// , "/app/main.css"
// ]

How do I recursively display all files and directories in a directory , This Java tutorial describes how to recursively traverse all the files and can be files or directories) in a directory File object via a call to its listFiles() method. everything below a certain directory including all subdirectories and their files, not  Unix recursive directory listing command. Since, not all versions of Linux, macOS, *BSD, and Unix-like system have -R option for the ls command. Try to use find command: find . -print. find /tmp/dir1 -print. find /tmp/dir1/ -print -ls. Recursive directory listing in Linux or Unix using the find command.

How to show recursive directory listing on Linux or Unix, How Do I Search My Linux and Unix Server For a File? How do I force delete a directory in Linux? Delete / Remove a Directory Linux Command  To list all sub files and sub directories nested in all levels, one solution is to use recursion algorithm: List files in the first level. For each file in the first level list files: If the file is a directory: Print out directory name. Repeat the step 1 and 2 with the current directory. If the file is a file: Print out file name. Continue

Java list directory - how to show directory contents in Java, walk() method returns a lazily populated stream of Paths by walking the file tree rooted at a given starting file. Files.walk() recursively walks all  This code tip shows how to use Java NIO API to recursively list all sub-directories and files in a given directory. The directory structure iterated in the below example is – C:\JavaBrahman\Level1 which has the structure, shown using the DOS command – DIR /s /b –

C program to list all files in a directory recursively, Logic to list all files and sub-directories of a directory in C programming. How to use opendir() , readdir() and closedir  However, sometimes we want to access everything below a certain directory including all subdirectories and their files, not just the immediate level of files. We can do with with recursion. The RecursiveFileDisplay class demonstrates how to do this. We obtain a File object representing the current directory and pass that File object to

Comments
  • So what exactly should the function return, can you post the expected result please? Is it an array of the file names?
  • What numbers? There are no numbers in the input. Should you make them up? How?
  • Here is what it is looking for according to the test error: you should be able to retrieve data from the server and return a sorted array of names
  • Then maybe you should ask your instructor
  • This seems to print them, not "return" them.
  • @Bergi added another method.
  • Thanks, that looks better to me :-) Unfortunately, OP doesn't seem to know exactly what he wants to do...