假设我们有一个包含四个文件夹的目录,如下所示:
- folder-1
- folder-2
- folder-3
- folder-4如何获取它们的路径并将它们存储在数组中,这样就可以得到如下数组:
['root/parent/folder-1', 'root/parent/folder-2', 'root/parent/folder-3', 'root/parent/folder-4']发布于 2016-03-08 13:07:09
这样做,如果您不需要绝对路径,省略__dirname,使用dirPath
var fs = require('fs');
var dirPath = 'parent/';
var result = []; //this is going to contain paths
fs.readdir(__dirname + dirPath, function (err, filesPath) {
if (err) throw err;
result = filesPath.map(function (filePath) {
return dirPath + filePath;
});
});发布于 2016-03-08 12:51:49
你可以通过fs.readdir来做
var fs = require('fs');
fs.readdir('root/parent/', function(err, files) {
if (err)
console.log(err);
else
files.map(function(f) {
return 'root/parent/'+f;
});
return files;
})https://stackoverflow.com/questions/35867745
复制相似问题