Node文件系统操作有哪些具体细节和步骤可以详细探讨?

2026-04-06 11:471阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计190个文字,预计阅读时间需要1分钟。

Node文件系统操作有哪些具体细节和步骤可以详细探讨?

javascript获取类型文件 // Joe于2017/9/22创建const fs=require('fs');const path=require('path');const getTypeFiles=(filePath, type=[], recursive=true)=> { let fileList=[]; let curPath=filePath; if (type.length===0) return fileList; // ... 省略部分代码 ...};

Node文件系统操作有哪些具体细节和步骤可以详细探讨?

Get Type Files

// Created by Joe on 2017/9/22. const fs = require('fs'); const path = require('path'); const getTypeFiles = (filePath, type = [], recursive = true) => { let fileList = [], curPath = filePath; if (type.length === 0) return fileList; let stat = fs.statSync(curPath); if (stat.isDirectory()) { let dirs = fs.readdirSync(curPath); for (let dir of dirs) { curPath = path.join(filePath, dir); stat = fs.statSync(curPath); if (stat.isFile()) { if (type.indexOf(path.extname(curPath)) !== -1) fileList.push(path.basename(curPath)); } else if (recursive) { fileList.push(...getTypeFiles(curPath, type)); } } } else { if (type.indexOf(path.extname(curPath)) !== -1) { fileList.push(path.basename(curPath)); } } return fileList; }; // module.exports = getTypeFiles; Create Directory

// create dir async const mdAsync = (dir) => { fs.exists(dir, (exists) => { if (!exists) { mdAsync(path.dirname(dir), () => { fs.mkdir(dir); }); } }); }; // create dir sync const mdSync = (dir) => { if (fs.existsSync(dir)) return true; if (mdSync(path.dirname(dir))) { fs.mkdirSync(dir); return true; } };

本文共计190个文字,预计阅读时间需要1分钟。

Node文件系统操作有哪些具体细节和步骤可以详细探讨?

javascript获取类型文件 // Joe于2017/9/22创建const fs=require('fs');const path=require('path');const getTypeFiles=(filePath, type=[], recursive=true)=> { let fileList=[]; let curPath=filePath; if (type.length===0) return fileList; // ... 省略部分代码 ...};

Node文件系统操作有哪些具体细节和步骤可以详细探讨?

Get Type Files

// Created by Joe on 2017/9/22. const fs = require('fs'); const path = require('path'); const getTypeFiles = (filePath, type = [], recursive = true) => { let fileList = [], curPath = filePath; if (type.length === 0) return fileList; let stat = fs.statSync(curPath); if (stat.isDirectory()) { let dirs = fs.readdirSync(curPath); for (let dir of dirs) { curPath = path.join(filePath, dir); stat = fs.statSync(curPath); if (stat.isFile()) { if (type.indexOf(path.extname(curPath)) !== -1) fileList.push(path.basename(curPath)); } else if (recursive) { fileList.push(...getTypeFiles(curPath, type)); } } } else { if (type.indexOf(path.extname(curPath)) !== -1) { fileList.push(path.basename(curPath)); } } return fileList; }; // module.exports = getTypeFiles; Create Directory

// create dir async const mdAsync = (dir) => { fs.exists(dir, (exists) => { if (!exists) { mdAsync(path.dirname(dir), () => { fs.mkdir(dir); }); } }); }; // create dir sync const mdSync = (dir) => { if (fs.existsSync(dir)) return true; if (mdSync(path.dirname(dir))) { fs.mkdirSync(dir); return true; } };