Snippets Collections
'use strict';

const {each} = require('async');
const {promisify} = require('util');
const fs = require('fs');

/**
 * Asynchronous Get All Files In Directory & Subdirectories
 * @param {string} dir - A path to a directory
 * @param {function|undefined} callback - Calls the function one time for each item in the folder.
 * @return {string[]} paths
 */
exports.async = (dir, callback = undefined) => {
  if (callback && typeof callback !== 'function') {
    throw new Error('Expected callback function');
  }

  const paths = [];

  try {
    startReading(dir, callback);
  } catch (error) {
    throw console.error(error);
  }

  return paths;

  async function startReading(dir, callback) {
    const items = await promisify(fs.readdir)(dir);

    each(items, async itemName => {
      const path = `${dir}/${itemName}`;

      const stat = await promisify(fs.stat)(path);

      if (stat.isDirectory()) return startReading(path, callback);

      if (callback) callback(path);

      paths.push(path);
    });
  }
};

/**
 * Synchronous Get All Files In Directory & Subdirectories
 * @param {string} dir - A path to a directory
 * @param {function|undefined} callback - Calls the function one time for each item in the folder.
 * @return {string[]} paths
 */
exports.sync = (dir, callback = undefined) => {
  if (callback && typeof callback !== 'function') {
    throw new Error('Expected callback function');
  }

  const paths = [];

  try {
    startReading(dir, callback);
  } catch (err) {
    throw console.error(err);
  }

  return paths;

  function startReading(dir, callback) {
    const items = fs.readdirSync(dir);

    each(items, itemName => {
      const path = `${dir}/${itemName}`;
      const isDirectory = fs.statSync(path).isDirectory();

      if (isDirectory) startReading(path, callback);

      else {
        if (callback) callback(path);

        paths.push(path);
      }
    });
  }
};
var fs = require('fs');

fs.createReadStream('test.log').pipe(fs.createWriteStream('newLog.log'));
star

Mon Aug 28 2023 18:48:53 GMT+0000 (Coordinated Universal Time) https://github.com/ANIname/recursive-dir-reader/blob/master/index.js

#fs
star

Mon Jun 08 2020 10:15:25 GMT+0000 (Coordinated Universal Time) https://lavalite.org/blog/fastest-method-to-copy-file-in-nodejs

#nodejs #fs #javascript

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension