snack.elve.club
snack.elve.club
最後のリネーム後のファイル名間違えて結構なファイルをロスした(ノД`)ナェルシク
上書きするから注意ね(ノД`)ナェルシク
googoleフォトから復活させる(ノД`)ナェルシク
処理が激重なので非同期処理にしたほうがいいかも。
※exiftoolインストールしてc:\nodeにコピって名前変更しておく。
const fs = require('fs');
const { spawnSync } = require('child_process');
function getMonthStr(path) {
const stats = fs.statSync(path);
let d = [];
d.push(new Date(stats.atime));
d.push(new Date(stats.mtime));
d.push(new Date(stats.ctime));
let tag = ["FileModifyDate", "FileAccessDate", "FileCreateDate", "CreateDate", "ModifyDate", "MetadataDate", "DateTimeOriginal", "CreateDate"];
let i;
let COMMAND = "C:\\node\\exiftool";
for (i of tag) {
let re = spawnSync(COMMAND, ["-T", "-" + i, path]);
if (re.stdout === null) {
console.log(re.error.toString());
} else {
let tmp = re.stdout.toString().replace("'", "");
tmp = tmp.replace(/\r?\n/g, "");
tmp = tmp.replace(":", "-");
if (tmp != "-") {
d.push(new Date(tmp));
}
}
}
let minD = new Date(Date.now());
for (i of d) {
if (minD.valueOf() > i.valueOf()) {
minD = i;
}
}
return ('00' + (minD.getMonth() + 1)).slice(-2);
}
function main() {
let basePath = "C:\\Users\\user\\OneDrive\\画像\\カメラ ロール\\";
let dirList = fs.readdirSync(basePath, {
withFileTypes: true,
}).filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name);
let yPath;
for (let i of dirList) {
yPath = basePath + i + "\\";
let fileList = fs.readdirSync(yPath, {
withFileTypes: true,
}).filter(dirent => dirent.isFile()).map(dirent => dirent.name);
for (let f of fileList) {
let fPath = yPath + f;
let m = getMonthStr(fPath);
if (Number(m) > 0 && Number(m) <= 12) {
fs.rename(fPath, yPath + m + "\\" + f, (err) => {
if (err) throw err;
console.log('ファイルを移動しました :' + fPath);
});
}
}
}
}
main();
あ、各フォルダ内に月のフォルダ作った部分忘れた(最初に作った後は不要だったので)
let yPath;
for (let i = 0; i < dirList.length; i++) {
yPath = dirPath + "\\" + dirList[i] + "\\";
for (let m = 1; m <= 12; m++) {
fs.mkdir(yPath + ('00' + m).slice(-2), (err) => {
if (err) {
console.log(err.toString());
}
});
}
}