xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Microsoft & Node.js All In One

Microsoft & Node.js All In One

Node.js read nested folders path

image

const fs = require("fs").promises;
const path = require("path");

const items = await fs.readdir("stores");
console.log(items); 

const items = await fs.readdir("stores", { withFileTypes: true });
for (let item of items) {
  const type = item.isDirectory() ? "folder" : "file";
  console.log(`${item.name}: ${type}`);
}

function findFiles(folderName) {
  const items = await fs.readdir(folderName, { withFileTypes: true });
  items.forEach((item) => {
    if (path.extname(item.name) === ".json") {
      console.log(`Found file: ${item.name} in folder: ${folderName}`);
    } else {
      // this is a folder, so call this method again and pass in
      // the path to the folder
      findFiles(path.join(folderName, item.name));
    }
  });
}

findFiles("stores");

https://github.com/MicrosoftDocs/node-essentials/blob/main/nodejs-files/index.js

Node.js &fs & node:fs & node:fs/promises

??? node.js modules & npm package ❓magic, index, folder path

import { open } from 'node:fs/promises';

let filehandle;
try {
  filehandle = await open('thefile.txt', 'r');
} finally {
  await filehandle?.close();
}

https://nodejs.org/api/fs.html#fspromisesreaddirpath-options

https://nodejs.org/api/fs.html#fsreaddirsyncpath-options

https://nodejs.dev/en/learn/reading-files-with-nodejs/

chown

import { chown } from 'node:fs';


https://nodejs.org/api/fs.html#fschownpath-uid-gid-callback

chmod

import { chmod } from 'node:fs';

chmod('my_file.txt', 0o775, (err) => {
  if (err) throw err;
  console.log('The permissions for file "my_file.txt" have been changed!');
});

https://nodejs.org/api/fs.html#fschmodpath-mode-callback

Error: EISDIR

Error: EISDIR: illegal operation on a directory, read


Server Error
Error: EISDIR: illegal operation on a directory, read

This error happened while generating the page. Any console logs will be displayed in the terminal window.

// fs

image

image

https://stackoverflow.com/questions/34959038/npm-stuck-giving-the-same-error-eisdir-illegal-operation-on-a-directory-read-a

refs

https://www.cnblogs.com/anonymous-ufo/p/16928886.html



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-11-27 21:45  xgqfrms  阅读(41)  评论(6编辑  收藏  举报