Get all files recursively in NodejS directories

I have a little problem with my function. I would like to get all the files in many directories. Currently, I can get the files in the file passed in the parameters. I would like to receive the html files of each folder in the folder passed as a parameter. I will explain if I add the parameter "test", I extract the files to "test", but I would like to get "test / 1 / *. Html", "test / 2 /./. Html":

var srcpath2 = path.join('.', 'diapo', result); function getDirectories(srcpath2) { return fs.readdirSync(srcpath2).filter(function (file) { return fs.statSync(path.join(srcpath2, file)).isDirectory(); }); } 

Result: [1,2,3]

thanks!

+5
source share
5 answers

It seems that the glob npm package will help you. Here is an example of how to use it:

File hierarchy:

 test ├── one.html └── test-nested └── two.html 

JS Code:

 var getDirectories = function (src, callback) { glob(src + '/**/*', callback); }; getDirectories('test', function (err, res) { if (err) { console.log('Error', err); } else { console.log(res); } }); 

which displays:

 [ 'test/one.html', 'test/test-nested', 'test/test-nested/two.html' ] 
+12
source

I needed something like this in an Electron application: get all the subfolders in a given base folder using TypeScript, and came up with this:

 import { readdirSync, statSync, existsSync } from "fs"; import * as path from "path"; // recursive synchronous "walk" through a folder structure, with the given base path getAllSubFolders = (baseFolder, folderList = []) => { let folders:string[] = readdirSync(baseFolder).filter(file => statSync(path.join(baseFolder, file)).isDirectory()); folders.forEach(folder => { folderList.push(path.join(baseFolder,folder)); this.getAllSubFolders(path.join(baseFolder,folder), folderList); }); } 
+2
source

You can also write your own code, as shown below, to go through the directory as shown below:

 var fs = require('fs'); function traverseDirectory(dirname, callback) { var directory = []; fs.readdir(dirname, function(err, list) { dirname = fs.realpathSync(dirname); if (err) { return callback(err); } var listlength = list.length; list.forEach(function(file) { file = dirname + '\\' + file; fs.stat(file, function(err, stat) { directory.push(file); if (stat && stat.isDirectory()) { traverseDirectory(file, function(err, parsed) { directory = directory.concat(parsed); if (!--listlength) { callback(null, directory); } }); } else { if (!--listlength) { callback(null, directory); } } }); }); }); } traverseDirectory(__dirname, function(err, result) { if (err) { console.log(err); } console.log(result); }); 

You can learn more about this here: http://www.codingdefined.com/2014/09/how-to-navigate-through-directories-in.html

0
source

Here is mine. Like all good answers, it's hard to understand:

 const isDirectory = path => statSync(path).isDirectory(); const getDirectories = path => readdirSync(path).map(name => join(path, name)).filter(isDirectory); const isFile = path => statSync(path).isFile(); const getFiles = path => readdirSync(path).map(name => join(path, name)).filter(isFile); const getFilesRecursively = (path) => { let dirs = getDirectories(path); let files = dirs .map(dir => getFilesRecursively(dir)) // go through each directory .reduce((a,b) => a.concat(b), []); // map returns a 2d array (array of file arrays) so flatten return files.concat(getFiles(path)); }; 
0
source
 const fs = require('fs'); const path = require('path'); var filesCollection = []; const directoriesToSkip = ['bower_components', 'node_modules', 'www', 'platforms']; function readDirectorySynchronously(directory) { var currentDirectorypath = path.join(__dirname + directory); var currentDirectory = fs.readdirSync(currentDirectorypath, 'utf8'); currentDirectory.forEach(file => { var fileShouldBeSkipped = directoriesToSkip.indexOf(file) > -1; var pathOfCurrentItem = path.join(__dirname + directory + '/' + file); if (!fileShouldBeSkipped && fs.statSync(pathOfCurrentItem).isFile()) { filesCollection.push(pathOfCurrentItem); } else if (!fileShouldBeSkipped) { var directorypath = path.join(directory + '\\' + file); readDirectorySynchronously(directorypath); } }); } readDirectorySynchronously(''); 

This will populate the filesCollection with all the files in the directory and its subdirectories (recursive). You have the option to skip some directory names in the directoriesToSkip array.

0
source

Source: https://habr.com/ru/post/1262286/


All Articles