[Node.js] 파일 생성 시간 확인[Node.js] 파일 생성 시간 확인

Posted at 2020. 8. 18. 17:22 | Posted in 카테고리 없음
반응형

// sort 참고

// https://ithub.tistory.com/67

// http://dudmy.net/javascript/2015/11/16/javascript-sort/







# 소스코드

const fs = require("fs");


// @see createfolder 폴더에 완성된 파일을 업로드한다.

fs.readdir("./createfolder", function(error, filelist) {

    filelist.forEach(function(file) {

        fs.stat("./createfolder/" + file, function (err, stats) {


            // @see GMT 날짜 데이터 포맷

            let createDate = new Date( stats.birthtimeMs );

            let fileInfo = { fileDate:createDate.ymdhms(), fileName:file };

            console.log(fileInfo);

        });

    });

});


// @see YYYY-MM-DD HH:MM:SS 형식으로 날짜 변경

Date.prototype.ymdhms = function() {

    const year = this.getFullYear().toString();

    const month = (this.getMonth() + 1).toString();

    const date = this.getDate().toString();

    const hour = this.getHours().toString();

    const minute = this.getMinutes().toString();

    const second = this.getSeconds().toString();

    return  year + "-" + (month[1] ? month : "0" + month[0]) + "-" + (date[1] ? date : "0" + date[0])

    + " "

    + (hour[1] ? hour : "0" + hour[0]) + ":" + (minute[1] ? minute : "0" + minute[0]) + ":" + (second[1] ? second : "0" + second[0]);

}




# 출력결과







const fs = require("fs");

const xlsx = require("xlsx");


// @see YYYY-MM-DD HH:MM:SS 형식으로 날짜 변경

Date.prototype.ymdhms = function() {

    const year = this.getFullYear().toString();

    const month = (this.getMonth() + 1).toString();

    const date = this.getDate().toString();

    const hour = this.getHours().toString();

    const minute = this.getMinutes().toString();

    const second = this.getSeconds().toString();

    return  year + "-" + (month[1] ? month : "0" + month[0]) + "-" + (date[1] ? date : "0" + date[0])

    + " "

    + (hour[1] ? hour : "0" + hour[0]) + ":" + (minute[1] ? minute : "0" + minute[0]) + ":" + (second[1] ? second : "0" + second[0]);

}


// 디렉토리 내의 파일을 검색하고 마지막 파일을 리턴한다.

function directoryFileListRead(callback) {


    fs.readdir("./createfolder", function(error, filelist) {


        let fileArr = new Array();

        let breakPoint = 0;


        filelist.forEach(function(file, idx, arr) {


            // 데이터를 일겅온다.

            fs.stat("./createfolder/" + file, function (err, stats) {


                fileArr[idx] = new Array();

                fileArr[idx] = { fileName:file, fileDate: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[0]);

                }

            });

        });

    });

}


directoryFileListRead(function( fileList ) {


    // 마지막 파일의 정보를 출력한다.

    console.log( fileList );

});






반응형
//