디렉토리 nodejs 내의 모든 디렉토리 가져 오기
나는 이것이 간단한 일이기를 바랐지만 그렇게할만한 것을 찾을 수 없었습니다.
주어진 폴더 / 디렉토리에있는 모든 폴더 / 디렉토리를 가져 오려고합니다.
예를 들어 :
<MyFolder>
|- SomeFolder
|- SomeOtherFolder
|- SomeFile.txt
|- SomeOtherFile.txt
|- x-directory
다음과 같은 배열을 얻을 것으로 예상됩니다.
["SomeFolder", "SomeOtherFolder", "x-directory"]
또는 위와 같은 방법으로 경로가 제공된 경우 ...
위의 작업을 수행하기 위해 이미 존재하는 것이 있습니까?
다음 은 현재 디렉토리의 모든 디렉토리 (숨겨 지거나 숨겨지지 않은)를 나열 할 수 있는 이 대답 의 더 짧고 동기화 된 버전입니다 .
const { lstatSync, readdirSync } = require('fs')
const { join } = require('path')
const isDirectory = source => lstatSync(source).isDirectory()
const getDirectories = source =>
readdirSync(source).map(name => join(source, name)).filter(isDirectory)
노드 10.10.0+에 대한 업데이트
새로운 withFileTypes
옵션을 사용 readdirSync
하여 추가 lstatSync
통화 를 건너 뛸 수 있습니다 .
const { readdirSync } = require('fs')
const getDirectories = source =>
readdirSync(source, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name)
JavaScript ES6 (ES2015) 구문 기능 덕분에 하나의 라이너입니다.
동기식 버전
const { readdirSync, statSync } = require('fs')
const { join } = require('path')
const dirs = p => readdirSync(p).filter(f => statSync(join(p, f)).isDirectory())
Node.js 10+의 비동기 버전 (실험)
const { readdir, stat } = require("fs").promises
const { join } = require("path")
const dirs = async path => {
let dirs = []
for (const file of await readdir(path)) {
if ((await stat(join(path, file))).isDirectory()) {
dirs = [...dirs, file]
}
}
return dirs
}
경로를 사용하여 디렉토리를 나열하십시오.
function getDirectories(path) {
return fs.readdirSync(path).filter(function (file) {
return fs.statSync(path+'/'+file).isDirectory();
});
}
재귀 솔루션
나는 모든 하위 디렉토리와 모든 하위 디렉토리 등을 얻는 방법을 찾기 위해 여기에 왔습니다. 허용 된 답변을 바탕으로 다음 과 같이 썼습니다.
const fs = require('fs');
const path = require('path');
function flatten(lists) {
return lists.reduce((a, b) => a.concat(b), []);
}
function getDirectories(srcpath) {
return fs.readdirSync(srcpath)
.map(file => path.join(srcpath, file))
.filter(path => fs.statSync(path).isDirectory());
}
function getDirectoriesRecursive(srcpath) {
return [srcpath, ...flatten(getDirectories(srcpath).map(getDirectoriesRecursive))];
}
이것은해야합니다 :
CoffeeScript (동기화)
fs = require 'fs'
getDirs = (rootDir) ->
files = fs.readdirSync(rootDir)
dirs = []
for file in files
if file[0] != '.'
filePath = "#{rootDir}/#{file}"
stat = fs.statSync(filePath)
if stat.isDirectory()
dirs.push(file)
return dirs
CoffeeScript (비동기)
fs = require 'fs'
getDirs = (rootDir, cb) ->
fs.readdir rootDir, (err, files) ->
dirs = []
for file, index in files
if file[0] != '.'
filePath = "#{rootDir}/#{file}"
fs.stat filePath, (err, stat) ->
if stat.isDirectory()
dirs.push(file)
if files.length == (index + 1)
cb(dirs)
자바 스크립트 (비동기)
var fs = require('fs');
var getDirs = function(rootDir, cb) {
fs.readdir(rootDir, function(err, files) {
var dirs = [];
for (var index = 0; index < files.length; ++index) {
var file = files[index];
if (file[0] !== '.') {
var filePath = rootDir + '/' + file;
fs.stat(filePath, function(err, stat) {
if (stat.isDirectory()) {
dirs.push(this.file);
}
if (files.length === (this.index + 1)) {
return cb(dirs);
}
}.bind({index: index, file: file}));
}
}
});
}
또는 외부 라이브러리를 사용할 수있는 경우을 사용할 수 있습니다 filehound
. 콜백, 약속 및 동기화 통화를 지원합니다.
약속 사용하기 :
const Filehound = require('filehound');
Filehound.create()
.path("MyFolder")
.directory() // only search for directories
.find()
.then((subdirectories) => {
console.log(subdirectories);
});
콜백 사용 :
const Filehound = require('filehound');
Filehound.create()
.path("MyFolder")
.directory()
.find((err, subdirectories) => {
if (err) return console.error(err);
console.log(subdirectories);
});
통화 동기화 :
const Filehound = require('filehound');
const subdirectories = Filehound.create()
.path("MyFolder")
.directory()
.findSync();
console.log(subdirectories);
자세한 정보 및 예는 다음 문서를 확인하십시오. https://github.com/nspragg/filehound
면책 조항 : 나는 저자입니다.
Node.js를 버전> = v10.13.0으로 fs.readdirSync는 배열 반환 fs.Dirent이 경우 오브젝트 withFileTypes
옵션으로 설정된다 true
.
사용할 수 있습니다.
const fs = require('fs')
const directories = source => fs.readdirSync(source, {
withFileTypes: true
}).reduce((a, c) => {
c.isDirectory() && a.push(c.name)
return a
}, [])
var getDirectories = (rootdir , cb) => {
fs.readdir(rootdir, (err, files) => {
if(err) throw err ;
var dirs = files.map(filename => path.join(rootdir,filename)).filter( pathname => fs.statSync(pathname).isDirectory());
return cb(dirs);
})
}
getDirectories( myDirectories => console.log(myDirectories));``
그리고 getDirectories의 비동기 버전에는 다음을위한 비동기 모듈 이 필요합니다 .
var fs = require('fs');
var path = require('path');
var async = require('async'); // https://github.com/caolan/async
// Original function
function getDirsSync(srcpath) {
return fs.readdirSync(srcpath).filter(function(file) {
return fs.statSync(path.join(srcpath, file)).isDirectory();
});
}
function getDirs(srcpath, cb) {
fs.readdir(srcpath, function (err, files) {
if(err) {
console.error(err);
return cb([]);
}
var iterator = function (file, cb) {
fs.stat(path.join(srcpath, file), function (err, stats) {
if(err) {
console.error(err);
return cb(false);
}
cb(stats.isDirectory());
})
}
async.filter(files, iterator, cb);
});
}
비동기 fs 호출을 약속하는 fs-extra 및 새로운 대기 비동기 구문 사용 :
const fs = require("fs-extra");
async function getDirectories(path){
let filesAndDirectories = await fs.readdir(path);
let directories = [];
await Promise.all(
filesAndDirectories.map(name =>{
return fs.stat(path + name)
.then(stat =>{
if(stat.isDirectory()) directories.push(name)
})
})
);
return directories;
}
let directories = await getDirectories("/")
적절한 오류 처리와 함께이 답변 의 CoffeeScript 버전 :
fs = require "fs"
{join} = require "path"
async = require "async"
get_subdirs = (root, callback)->
fs.readdir root, (err, files)->
return callback err if err
subdirs = []
async.each files,
(file, callback)->
fs.stat join(root, file), (err, stats)->
return callback err if err
subdirs.push file if stats.isDirectory()
callback null
(err)->
return callback err if err
callback null, subdirs
비동기에 따라 다름
또는 이것을 위해 모듈을 사용하십시오! (모든 것을위한 모듈이 있습니다. [인용 필요])
모든 async
버전 을 사용해야하는 경우 이런 식으로 할 수 있습니다.
디렉토리 길이를 기록하고이를 표시기로 사용하여 모든 비동기 상태 작업이 완료되었는지 여부를 알려줍니다.
비동기 통계 작업이 완료되면 모든 파일 통계가 확인되었으므로 콜백을 호출하십시오.
Node.js가 단일 스레드 인 경우에만 작동합니다. 두 개의 비동기 작업이 동시에 카운터를 증가시키지 않는다고 가정하기 때문입니다.
'use strict';
var fs = require("fs");
var path = require("path");
var basePath = "./";
function result_callback(results) {
results.forEach((obj) => {
console.log("isFile: " + obj.fileName);
console.log("fileName: " + obj.isFile);
});
};
fs.readdir(basePath, (err, files) => {
var results = [];
var total = files.length;
var finished = 0;
files.forEach((fileName) => {
// console.log(fileName);
var fullPath = path.join(basePath, fileName);
fs.stat(fullPath, (err, stat) => {
// this will work because Node.js is single thread
// therefore, the counter will not increment at the same time by two callback
finished++;
if (stat.isFile()) {
results.push({
fileName: fileName,
isFile: stat.isFile()
});
}
if (finished == total) {
result_callback(results);
}
});
});
});
보시다시피, 이것은 "깊이 우선"접근 방식이며 콜백 지옥을 초래할 수 있으며 "기능적"이 아닙니다. 사람들은 비동기 작업을 Promise 개체에 래핑하여 Promise로이 문제를 해결하려고합니다.
'use strict';
var fs = require("fs");
var path = require("path");
var basePath = "./";
function result_callback(results) {
results.forEach((obj) => {
console.log("isFile: " + obj.fileName);
console.log("fileName: " + obj.isFile);
});
};
fs.readdir(basePath, (err, files) => {
var results = [];
var total = files.length;
var finished = 0;
var promises = files.map((fileName) => {
// console.log(fileName);
var fullPath = path.join(basePath, fileName);
return new Promise((resolve, reject) => {
// try to replace fullPath wil "aaa", it will reject
fs.stat(fullPath, (err, stat) => {
if (err) {
reject(err);
return;
}
var obj = {
fileName: fileName,
isFile: stat.isFile()
};
resolve(obj);
});
});
});
Promise.all(promises).then((values) => {
console.log("All the promise resolved");
console.log(values);
console.log("Filter out folder: ");
values
.filter((obj) => obj.isFile)
.forEach((obj) => {
console.log(obj.fileName);
});
}, (reason) => {
console.log("Not all the promise resolved");
console.log(reason);
});
});
fs 、 path 모듈을 사용 하면 폴더를 얻을 수 있습니다. 이 약속을 사용합니다. 채우기, 당신의 캔 변경하여 얻을 것이다 경우 isDirectory () 로 ) (ISFILE를 FS - - Nodejs를 fs.Stats .At을 마지막으로, 당신은 file'name의 file'extname을 얻을 수있는 등 Nodejs --- 경로
var fs = require("fs"),
path = require("path");
//your <MyFolder> path
var p = "MyFolder"
fs.readdir(p, function (err, files) {
if (err) {
throw err;
}
//this can get all folder and file under <MyFolder>
files.map(function (file) {
//return file or folder path, such as **MyFolder/SomeFile.txt**
return path.join(p, file);
}).filter(function (file) {
//use sync judge method. The file will add next files array if the file is directory, or not.
return fs.statSync(file).isDirectory();
}).forEach(function (files) {
//The files is array, so each. files is the folder name. can handle the folder.
console.log("%s", files);
});
});
이 답변은 readdirSync
또는 과 같은 차단 기능을 사용하지 않습니다 statSync
. 외부 의존성을 사용하지 않으며 콜백 지옥의 깊이에서 스스로를 찾지 않습니다.
대신 약속 및 async-await
구문 과 같은 최신 JavaScript 편의를 사용 합니다. 그리고 비동기 결과는 병렬로 처리됩니다. 순차적으로-
const { readdir, stat } =
require ("fs") .promises
const { join } =
require ("path")
const dirs = async (path = ".") =>
(await stat (path)) .isDirectory ()
? Promise
.all
( (await readdir (path))
.map (p => dirs (join (path, p)))
)
.then
( results =>
[] .concat (path, ...results)
)
: []
예제 패키지를 설치 한 다음 함수를 테스트하겠습니다.
$ npm install ramda
$ node
그것이 작동하는 것을 보자-
> dirs (".") .then (console.log, console.error)
[ '.'
, 'node_modules'
, 'node_modules/ramda'
, 'node_modules/ramda/dist'
, 'node_modules/ramda/es'
, 'node_modules/ramda/es/internal'
, 'node_modules/ramda/src'
, 'node_modules/ramda/src/internal'
]
일반화 된 모듈을 사용하면 Parallel
, 우리의 정의를 단순화 할 수 있습니다 dirs
-
const Parallel =
require ("./Parallel")
const dirs = async (path = ".") =>
(await stat (path)) .isDirectory ()
? Parallel (readdir (path))
.flatMap (f => dirs (join (path, f)))
.then (results => [ path, ...results ])
: []
Parallel
위에서 사용한 모듈은 유사한 문제를 해결하기위한 함수의 세트로부터 추출 된 패턴이었다. 자세한 설명은 관련 Q & A를 참조하십시오 .
다른 사람이 웹 검색으로 여기에 와서 Grunt가 이미 종속성 목록에있는 경우에 대한 대답은 사소합니다. 내 해결책은 다음과 같습니다.
/**
* Return all the subfolders of this path
* @param {String} parentFolderPath - valid folder path
* @param {String} glob ['/*'] - optional glob so you can do recursive if you want
* @returns {String[]} subfolder paths
*/
getSubfolders = (parentFolderPath, glob = '/*') => {
return grunt.file.expand({filter: 'isDirectory'}, parentFolderPath + glob);
}
또 다른 재귀 접근법
에 대해 알게 된 Mayur에게 감사합니다 withFileTypes
. 특정 폴더의 파일을 재귀 적으로 얻기 위해 다음 코드를 작성했습니다. 디렉토리 만 가져 오도록 쉽게 수정할 수 있습니다.
const getFiles = (dir, base = '') => readdirSync(dir, {withFileTypes: true}).reduce((files, file) => {
const filePath = path.join(dir, file.name)
const relativePath = path.join(base, file.name)
if(file.isDirectory()) {
return files.concat(getFiles(filePath, relativePath))
} else if(file.isFile()) {
file.__fullPath = filePath
file.__relateivePath = relativePath
return files.concat(file)
}
}, [])
기능 프로그래밍
const fs = require('fs')
const path = require('path')
const R = require('ramda')
const getDirectories = pathName => {
const isDirectory = pathName => fs.lstatSync(pathName).isDirectory()
const mapDirectories = pathName => R.map(name => path.join(pathName, name), fs.readdirSync(pathName))
const filterDirectories = listPaths => R.filter(isDirectory, listPaths)
return {
paths:R.pipe(mapDirectories)(pathName),
pathsFiltered: R.pipe(mapDirectories, filterDirectories)(pathName)
}
}
ES6의 완전 비동기 버전, 기본 패키지 fs.promises 및 async / await 만 파일 작업을 병렬로 수행합니다.
const fs = require('fs');
const path = require('path');
async function listDirectories(rootPath) {
const fileNames = await fs.promises.readdir(rootPath);
const filePaths = fileNames.map(fileName => path.join(rootPath, fileName));
const filePathsAndIsDirectoryFlagsPromises = filePaths.map(async filePath => ({path: filePath, isDirectory: (await fs.promises.stat(filePath)).isDirectory()}))
const filePathsAndIsDirectoryFlags = await Promise.all(filePathsAndIsDirectoryFlagsPromises);
return filePathsAndIsDirectoryFlags.filter(filePathAndIsDirectoryFlag => filePathAndIsDirectoryFlag.isDirectory)
.map(filePathAndIsDirectoryFlag => filePathAndIsDirectoryFlag.path);
}
테스트를 거쳤습니다.
비동기 / 대기 변형 :
async function getFolders(path) {
let result = Array();
let files = await fs.readdir(path);
for (let i = 0; i < files.length; i++) {
var filePath = path + '/' + file;
if (await fs.stat(filePath).isDirectory()) {
result.push(filePath);
}
}
return result;
}
fs. 대신 fs-extra 를 사용하는 것이 좋습니다 ..
참고 URL : https://stackoverflow.com/questions/18112204/get-all-directories-within-directory-nodejs
'Programing' 카테고리의 다른 글
왜 우리는 C 연합이 필요합니까? (0) | 2020.04.24 |
---|---|
ActionMailer보기에서보기 도우미를 사용하는 방법은 무엇입니까? (0) | 2020.04.24 |
Postman Chrome 확장 프로그램으로 배열을 보낼 수 있습니까? (0) | 2020.04.24 |
Spark에서 DataFrame, Dataset 및 RDD의 차이점 (0) | 2020.04.24 |
루프 내부 또는 외부에서 변수 선언 (0) | 2020.04.24 |