Home > Article > Web Front-end > How to implement fs file system directory operations and file information operations in node.js?
This article gives you a detailed analysis of the fs file system directory operation and file information operation methods in node.js, as well as a detailed code explanation. Readers in need can refer to it.
If the directory exists, the creation fails
Synchronously create the directory fs.mkdirSync(path, [ mode])
const fs = require('fs'); let mkdir = './mkdir'; fs.mkdir(mkdir, (err) => { if (err) { console.log(`mkdir ${mkdir} file failed~`); } else { console.log(`mkdir ${mkdir} file success~`); } });
If there are subdirectories or subfiles in the directory being read, the subdirectories or subdirectories will be The file name of the sub-file is used as the array element of files
Read the directory synchronously fs.readdirSync()
const fs = require('fs'); let mkdir = './mkdir'; fs.mkdir(mkdir, (err) => { if (err) { console.log(`mkdir ${mkdir} file failed~`); return false; } console.log(`mkdir ${mkdir} file success~`); let fileName = ['ONE', 'TWO', 'THREE']; fileName.forEach((elem) => { fs.mkdir(`${mkdir}/${elem}`, (err) => { if (err) { console.log(`${mkdir}/${elem} failed~`); return false; } }); fs.readdir(mkdir, (err, files) => { if (err) { console.log(`readdir ${mkdir} file failed~`); return false; } console.log(`readdir ${mkdir} file success~`); console.log(`${files}`); }); }); });
View and modify File or directory information
#In the fs module, you can use the stat method or lstat method to view a file or directory. The only difference is that when viewing information about symbolic link files, you must use the lstat method.
fs.stat(path, callback(err, stats))
Synchronization method to view file information fs.statSync(path);
const fs = require('fs'); let mkdir = './mkdir'; fs.stat(mkdir, (err, stats) => { if (err) { console.log(`fs.stats ${mkdir} file failed~`); } else { console.log(`fs.stats ${mkdir} file success~`); console.log(stats); } });
stats detailed explanation
Stats { dev: 2050,文件或目录所在的设备ID,仅在UNIX有效 mode: 16877,使用数值形式代表的文件或目录的权限标志 nlink: 3,文件或目录的硬连接数量 uid: 0,文件或目录的所有者的用户ID,仅在UNIX有效 gid: 0,文件或目录的所有者的用户组ID,仅在UNIX有效 rdev: 0,为字符设备文件或块设备文件所在设备ID,仅在UNIX有效 blksize: 4096, ino: 4197533,文件或目录的索引编号,仅在UNIX有效 size: 4096,文件尺寸,即文件中的字节数 blocks: 8, atimeMs: 1511846425357.986, mtimeMs: 1511846425256.986, ctimeMs: 1511846425256.986, birthtimeMs: 1511846425256.986, atime: 2017-11-28T05:20:25.358Z,文件的访问时间 mtime: 2017-11-28T05:20:25.257Z,文件的修改时间 ctime: 2017-11-28T05:20:25.257Z,文件的创建时间 birthtime: 2017-11-28T05:20:25.257Z }
fstat method to query file information
const fs = require('fs'); let mkdir = './mkdir'; fs.open(mkdir, 'r', (err, fd) => { if (err) { console.log(`open ${mkdir} file failed~`); return false; } fs.fstat(fd, (err, stats) => { if (err) { console.log(`fstat ${mkdir} file failed~`); return false; } console.log(JSON.stringify(stats)); }) })
fs.fstat
{ "dev": 1041887651, "mode": 16822, "nlink": 1, "uid": 0, "gid": 0, "rdev": 0, "ino": 4222124650663107, "size": 0, "atimeMs": 1519394418412.3062, "mtimeMs": 1519394418412.3062, "ctimeMs": 1519394418412.3062, "birthtimeMs": 1519394418402.2554, "atime": "2018-02-23T14:00:18.412Z", "mtime": "2018-02-23T14:00:18.412Z", "ctime": "2018-02-23T14:00:18.412Z", "birthtime": "2018-02-23T14:00:18.402Z" }Check whether the file or directory exists
const fs = require('fs'); let mkdir = './mkdir'; fs.exists(mkdir, (exits) => { if (exits) { console.log(`${exits}, ${mkdir} file exists`); } else { console.log(`${exits}, ${mkdir} file not exists`) } });
Modify the file access time and modification time
// 修改文件访问时间及修改时间都为当前时间 const fs = require('fs'); let mkdir = './mkdir'; fs.utimes(mkdir, new Date(), new Date(), (err) => { if (err) { console.log(`fs.utimes ${mkdir} file failed~`); } else { console.log(`fs.utimes ${mkdir} file success~`); } })
Modify the permissions of a file or directory
const fs = require('fs'); let mkdir = './mkdir'; fs.chmod(mkdirOne, '0600', (err) => { if (err) { console.log(`fs.chmod ${mkdir} file failed`); return false; } console.log(`fs.chmod ${mkdir} file success~`); });After using the open method or openSync method to open the file and return the file descriptor, you can use the fchmod method in the fs module to modify the read and write permissions of the file
const fs = require('fs'); let mkdir = './mkdir'; fs.open(mkdir, 'r', (err, fd) => { if (err) { console.log(`open file ${mkdir} failed~`); return false; } fs.fchmod(fd, '0600', (err) => { if (err) { console.log(`fs.fchmod ${mkdir} file failed~`); return false; } console.log(`fs.fchmod ${mkdir} file success~`); }) });The above is what I compiled for everyone Yes, I hope it will be helpful to everyone in the future. Related articles:
How to create an instruction to upload photos in AngularJS (detailed tutorial)
How to do it in javaScript Dynamically adding instances of Li elements
How to modify the default style in elementui?
The above is the detailed content of How to implement fs file system directory operations and file information operations in node.js?. For more information, please follow other related articles on the PHP Chinese website!