Skip to content

Instantly share code, notes, and snippets.

@naosim
Created December 18, 2018 02:02
Show Gist options
  • Save naosim/6d7da28fcbdb3282f1bf95d789516ed0 to your computer and use it in GitHub Desktop.
Save naosim/6d7da28fcbdb3282f1bf95d789516ed0 to your computer and use it in GitHub Desktop.
ディレクトリ配下で最後に更新したファイルを取得する
// 最後に更新されたファイルを取得する
// 利用例
// ls -l | node lastupdate.js
// => 2018-12-18 10:50 ./worklog/index.js
//
// サブディレクトリも対象にするならこう
// find . -type f | xargs ls -l | node worklog index.js
//
// 事前準備
// npmで'textpipe'を入れておくこと
var textpipe = require('textpipe');
const zerofil2 = (v) => `0${v}`.slice(-2)
const now = new Date()
const ditectYear = (month) => now.getFullYear() + (now.getMonth() + 1 >= month ? 0 : -1)
var lastUpdateFile = null;
textpipe.eachLine(function(line) {
const ary = line.split(' ').filter(v => v.trim().length > 0)
if(ary[7].indexOf(':') == -1) { // 更新日が遠い場合、時刻に西暦が入っているのでそれを除外する
return;
}
const obj = {
date: new Date(`${ditectYear(parseInt(ary[5]))}-${zerofil2(ary[5])}-${zerofil2(ary[6])} ${ary[7]}`),
file: ary[8]
}
if(!lastUpdateFile || obj.date.getTime() > lastUpdateFile.date.getTime()) {
lastUpdateFile = obj;
}
},
function() {
const ymd = `${lastUpdateFile.date.getFullYear()}-${lastUpdateFile.date.getMonth() + 1}-${lastUpdateFile.date.getDate()}`;
const time = `${lastUpdateFile.date.getHours()}:${lastUpdateFile.date.getMinutes()}`;
console.log(ymd, time, lastUpdateFile.file);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment