获取目录nodejs内的所有目录 [英] Get all directories within directory nodejs
本文介绍了获取目录nodejs内的所有目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望这将是一件简单的事情,但是我找不到任何东西。
I was hoping this would be a simple thing, but I cannot find anything out there to do so.
我只是希望得到一个给定的文件夹/目录。
I just want to get all folders/directories within a given folder/directory.
所以例如:
<MyFolder>
|- SomeFolder
|- SomeOtherFolder
|- SomeFile.txt
|- SomeOtherFile.txt
|- x-directory
我希望得到一个数组:
["SomeFolder", "SomeOtherFolder", "x-directory"]
或上述有路径,如果这是如何服务...
Or the above with the path if that was how it was served...
所以还有什么已经存在做上述?
So does anything already exist to do the above?
推荐答案
这是一个较短的同步版本的此答案,可以列出所有目录(隐藏或不存在)在当前目录中:
Here's a shorter, syncronous version of this answer that can list all directories (hidden or not) in the current directory:
const fs = require('fs')
const path = require('path')
function getDirectories (srcpath) {
return fs.readdirSync(srcpath)
.filter(file => fs.lstatSync(path.join(srcpath, file)).isDirectory())
}
这篇关于获取目录nodejs内的所有目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文