Microsoft & Node.js All In One
Node.js read nested folders path
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
<iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/3xm7spsmtRg?start=327" title="YouTube video player" width="560"></iframe>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.
https://www.cnblogs.com/anonymous-ufo/p/16928886.html
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载
标签:Node,node,fs,js,item,https,path,Microsoft From: https://www.cnblogs.com/xgqfrms/p/16930760.html