使用JavaScript / nodejs计算目录中的文件数? [英] Count the number of files in a directory using JavaScript/nodejs?
本文介绍了使用JavaScript / nodejs计算目录中的文件数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用纯粹的JavaScript或包使用 nodejs
计算目录中的文件数?我想做这样的事情:
How can I count the number of files in a directory using nodejs
with just plain JavaScript or packages? I want to do something like this:
或者在bash脚本中我会做这个:
Or in bash script I'd do this:
getLength() {
DIRLENGTH=1
until [ ! -d "DIR-$((DIRLENGTH+1))" ]; do
DIRLENGTH=$((DIRLENGTH+1))
done
}
推荐答案
使用 fs
,我发现检索目录文件计数很简单。
Using fs
, I found retrieving the directory file count to be straightforward.
const fs = require('fs');
const dir = './directory';
fs.readdir(dir, (err, files) => {
console.log(files.length);
});
这篇关于使用JavaScript / nodejs计算目录中的文件数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文