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

refs

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.


image

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



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 發佈文章使用:只允許註冊用戶纔可以訪問!

原創文章,版權所有©️xgqfrms, 禁止轉載 🈲️,侵權必究⚠️!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章