简体   繁体   English

NodeJs Glob 不在 Windows 上的驱动器磁盘中显示文件/文件夹

[英]NodeJs Glob dont show files/folders in Drive Disk on Windows

I want to get a folders hierarchy tree from the Drive Disk until the last subfolder.我想从驱动器磁盘中获取文件夹层次结构树,直到最后一个子文件夹。

Disk Drive
-First Folder
---Subfolder One
---Subfolder Two
-Second Folder
---Subfolder One
-Nth Folder
---Subfolder One
---Subfolder N

I will input what folder I want to inspect (this part is working in glob):我将输入我要检查的文件夹(这部分在 glob 中工作):

    let mg = new Glob('C:/Program Files/*', { mark: true }, function (err, matches) {
        if (err) {
            console.log(err);
        } else {
            console.log('folders', matches);
        }
    });

Glob will not show what folders are in a Disk Drive: Glob 不会显示磁盘驱动器中的文件夹:

    let mg = new Glob('C:/*', { mark: true }, function (err, matches) {
        if (err) {
            console.log(err);
        } else {
            console.log('folders', matches);
        }
    });

Res:回复:

files []

I have found a solution.我找到了解决办法。 I used child_process to call cmd commands and find the subfolders of the Local Drives.我使用child_process调用 cmd 命令并找到本地驱动器的子文件夹。 I think child_process has a promise module, when I will find a cleaner version of the code I will update.我认为child_process有一个 promise 模块,当我找到我将更新的代码的更清晰版本时。

const child = require('child_process');

let pathLetter: '';

function asyncGetAllFolders() {
  return new Promise((resolve) => {
    child.exec(
      `cd /d ${pathLetter}:/ && dir ${pathLetter}: /b /ad`,
      (error, stdout) => {
        if (error) {
          console.log(error);
          next(error);
        } else {
          allFoldersList = stdout.split('\r\n').map((value) => value.trim());
          resolve('done!');
        }
      }
    );
  });
}

function asyncGetHiddenFolders() {
  return new Promise((resolve) => {
    child.exec(
      `cd /d ${pathLetter}:/ && dir ${pathLetter}: /b /ah`,
      (error, stdout) => {
        if (error) {
          next(error);
        } else {
          hiddenFolderList = stdout
            .split('\r\n')
            .map((value) => value.trim());
          resolve('done!');
        }
      }
    );
  });
}

async function extractUniqueFolders() {
  await asyncGetAllFolders();
  await asyncGetHiddenFolders();

  hiddenFolderList.forEach((item) => {
    if (allFoldersList.indexOf(item) !== -1) {
      allFoldersList.splice(allFoldersList.indexOf(item), 1);
    }
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM