How to use the promiseFiles function from node-dir

Find comprehensive JavaScript node-dir.promiseFiles code examples handpicked from public code repositorys.

node-dir.promiseFiles is a function provided by the node-dir module in Node.js that recursively reads a directory and returns a promise that resolves with an array of file paths.

589
590
591
592
593
594
595
596
597
598
  let appIconsFolder = path.resolve(PieMenuFolder,'icons');    
  return nodeDir.promiseFiles(appIconsFolder);
},
getUserIcons: function(){    
  let userIconsFolder = path.resolve(UserDataFolder,'icons');
  return nodeDir.promiseFiles(userIconsFolder);
},
getLocalIconPath:function(iconFilepath){
  let appIconsFolder = path.resolve(PieMenuFolder,'icons');
  let userIconsFolder = path.resolve(UserDataFolder,'icons');
fork icon7
star icon104
watch icon0

69
70
71
72
73
74
75
76
77
78
79
}


exports.lsAttachments = async (dataset) => {
  const dirName = exports.attachmentsDir(dataset)
  if (!await fs.pathExists(dirName)) return []
  const files = (await dir.promiseFiles(dirName))
    .map(f => path.relative(dirName, f))
  return files.filter(p => path.basename(p).toLowerCase() !== 'thumbs.db')
}

fork icon6
star icon26
watch icon4

+ 7 other calls in file

How does node-dir.promiseFiles work?

node-dir.promiseFiles works by recursively scanning a directory and its subdirectories for files, and returning a promise that resolves with an array of the file paths found.

The function takes two arguments: the directory to scan and an optional options object that can be used to control the behavior of the scan, such as whether to follow symbolic links or filter the results by a file extension.

The promise that is returned is resolved with an array of file paths found in the directory and its subdirectories.

The function uses Node.js' built-in fs module to perform the file system operations needed to scan the directory and subdirectories.

This makes it useful for situations where you need to scan a directory and find all of the files contained within it, such as when building a file indexing system or when performing batch operations on a large number of files.

194
195
196
197
198
199
200
201
202
203

const dirs = process.env['plots_dir'].split(':');
for (let p = 0; p < dirs.length; p++) {
  const dirPath = dirs[p];
  try {
    const allFiles = await dir.promiseFiles(dirPath);
    const plotFiles = allFiles.filter(f => f.endsWith('.plot'));
    for (let i = 0; i < plotFiles.length; i++) {
      const bSize = await stat(plotFiles[i]);
      const gSize = bSize / G_SIZE;
fork icon5
star icon15
watch icon0

2
3
4
5
6
7
8
9
10
11
const replaceExt = require('replace-ext');
const upath = require('upath');
module.exports = function (api, params) {
   const apiMap = {};
   const apiAbsoluteDir = path.join(__dirname, '../api');
   return dir.promiseFiles(apiAbsoluteDir).then(files => {
      return files.map(file => {
         return replaceExt(path.relative(apiAbsoluteDir, file), '');
      }).filter(path => path !== 'index').forEach(api => {
         apiMap[upath.toUnix(api)] = path.join(__dirname, './', api);
fork icon1
star icon3
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
const dir = require("node-dir");

// Recursively scan a directory for files
dir
  .promiseFiles("/path/to/directory")
  .then((files) => {
    console.log(`Found ${files.length} files:`);
    console.log(files);
  })
  .catch((error) => {
    console.error(`Error: ${error.message}`);
  });

In this example, we use dir.promiseFiles to recursively scan the directory located at /path/to/directory and return a promise that resolves with an array of file paths found in the directory and its subdirectories. We use .then() to attach a callback that will be executed when the promise is resolved with the array of file paths. We log the number of files found and the file paths to the console. We use .catch() to attach a callback that will be executed if the promise is rejected with an error, and log the error message to the console. When the code is run, the output will be: less Copy code

25
26
27
28
29
30
31
32
33
34
/**
 * 
 * @param {FileList} files 
 */
async function list_files(files){
    return dir.promiseFiles(files)
      .then(file => {
        file = file.filter(_file => {
            return _file.indexOf('node_modules') === -1 && 
            (['js', 'jsx', 'ts', 'html', 'htm', 'tsx'].includes(extension(_file)) || 
fork icon0
star icon1
watch icon0

8
9
10
11
12
13
14
15
16
17
18
19


// reset("cp_dirs.json");
// reset("cp_files.json");


(async () => {
    const { files, dirs } = await dir.promiseFiles(PATH, 'all')
    filesArr = extract(files)
    dirsArr = extract(dirs)
    await translateFile(dirsArr, CP_DIRS)
    await translateFile(filesArr, CP_FILES)
fork icon0
star icon0
watch icon0

3
4
5
6
7
8
9
10
11
12
const {spawn} = require("child_process")
const {Notification} = require("electron")
const getAllParameters = (event,repositoryRoot) => {
    try {
        const paramFolder = path.join(repositoryRoot,"Parameters")
        dir.promiseFiles(paramFolder)
        .then(paramPaths => {
            paramPaths = paramPaths.filter(path => {
                path = path.replace(repositoryRoot+"\\","")
                const splitPath = path.split("\\")
fork icon0
star icon0
watch icon0

+ 3 other calls in file

153
154
155
156
157
158
159
160
161
162
    mainWindow.webContents.send('data-received', "Some more data")
})

ipcMain.on("get-reports-explorer", async (event) => {
      try {
      dir.promiseFiles(reportsRoot)
        .then(files => {
            if(files)
                event.reply('reports-explorer', files,reportsRoot) 
        })
fork icon0
star icon0
watch icon0

50
51
52
53
54
55
56
57
58
59
 * Get all job file paths
 * @return {Promise<String>} File paths
 */
_jobFilePaths() {
	const consumerPath = this._config.get('queue.consumerPath');
	return dir.promiseFiles(consumerPath ? consumerPath : this._helpers.appRoot() + '/app/Jobs/Consumers');
}

/**
 * Require all available jobs and process them
fork icon0
star icon0
watch icon0