[Node.js] 디렉토리내의 파일 정보 가져오기[Node.js] 디렉토리내의 파일 정보 가져오기
Posted at 2020. 9. 7. 01:18 | Posted in Node.js■ 폴더내의 파일리스트 정보 가져오기
# 소스코드
directory_list.js |
const fs = require( "fs" ); function directoryFileListRead( callback ) { fs.readdir( "./testfolder", function( error, filelist ) { const fileArr = new Array(); let breakPoint = 0; filelist.forEach( function( file, idx, arr ) { fs.stat("./testfolder/" + file, function ( err, stats ) { fileArr.push( { fileName : file, birthTime : stats.birthtimeMs } ); // 반복문 실행회수를 더한다. breakPoint++; // 배열의 수와 반복문이 실행된 회수가 같은면, 마지막 값까지 반복문이 실행되면 콜백한다. if( arr.length === breakPoint ) { // 오름차순 정렬 // fileArr.sort(function(a, b) { return a.fileDate < b.fileDate ? -1 : a.fileDate > b.fileDate ? 1 : 0; }); // 내림차순 정렬 fileArr.sort(function(a, b) { return a.fileDate > b.fileDate ? -1 : a.fileDate < b.fileDate ? 1 : 0; }); callback( fileArr ); } }); }); }); } directoryFileListRead( function( fileArr ) { console.log( fileArr ); }); |
# 출력결과
$ node directory_list.js |
'Node.js' 카테고리의 다른 글
[Node.js] exceljs를 이용하여 Excel 파일 읽기 (0) | 2020.08.20 |
---|---|
[Node.js] HTTPS 서버 구현하기 (0) | 2020.05.20 |
[Node.js] socket 통신으로 파일 업로드 하기 (0) | 2020.05.14 |
[Noe.js] 실행시 값 입력하기 (0) | 2020.05.04 |
[Node.js] Email 발송하기 (0) | 2020.05.01 |