Skip to content

EN_scanFolders

Francisco M. E edited this page Jan 21, 2019 · 5 revisions

About

This method allow to read from JavaScript a tree directory from an start point, be careful, if select the root directory it's method can work several minutes if you have to many folders and files.

It's work in Linux, MAX or Windows.

Code sample


const fs = require('fs');
const { scanFolders } = require('fs-hard-drive');
const { scanFoldersInfo } = require('fs-hard-drive');

let test = scanFolders('D:\\demofolder');

test
        .promise
        .then(() => {
            let testParsed = JSON.stringify(scanFoldersInfo[test.key]);
            fs.writeFile('sample.json', testParsed, function (err) {
                if (err) throw err;
                console.log('Result saved!');
            });
        })
        .catch((err) => {
            console.log('err: ', err);
        });

console.log('Other task while scaning is working');

Definitions

The method return an object with two properties

  • promise: It's control when scan will be finished
  • key: Define the propertie into scanFoldersInfo for read the scan-data

into scanFoldersInfo[key] it's defined the object "Scan" with the result of it's, can be query while scaning is runing.

Scan (object)

  • types: File type count thats appear in the scan
  • global: object
    • files: num files finded
    • size: size in bytes
    • start: Date object
    • end: Date object
  • tree: FolderInfo object

FolderInfo (object)

  • error: if have some error
  • info: object
    • files: num files finded
    • size: size in bytes
  • folders: [FolderEntry] Folders into it
  • files: [FileEntry] Files into into it
  • others: [FileEntry] other elements
  • not_readable: [FileEntry]

FolderEntry (object)

  • error: if have some error
  • name: string
  • path: string
  • size: int bytes
  • children: [FolderInfo]

FileEntry (object)

  • error: if have some error
  • name: string
  • path: string
  • size: int bytes