'use strict';
const fs = require('fs');
const p = console.log;
const rename = (fs, oldFilePath, newFilePath) => {
fs.rename(oldFilePath, newFilePath, (err) => {
err ? p(err) : null;
});
};
const getPaddedStr = (n) => {
if (n < 10) {
return '00' + n;
}
if (n < 100) {
return '0' + n;
}
return '' + n;
};
/** image-1.jpg -> 100.jpg */
const getNewFileName = (oldFileName) => {
const _1_jpg = oldFileName.split('-')[1];
const _1_jpgArr = _1_jpg.split('.');
const num = _1_jpgArr[0];
const ext = _1_jpgArr[1];
const newFileName = getPaddedStr(num) + '.' + ext;
return newFileName;
};
const renameForFiles = (rPath, files) => {
files.forEach((oldFileName) => {
const newFileName = getNewFileName(oldFileName);
const oldFilePath = rPath + '/' + oldFileName;
const newFilePath = rPath + '/' + newFileName;
rename(fs, oldFilePath, newFilePath);
p(oldFilePath + ' => ' + newFilePath);
});
};
const rPath = './testdir';
fs.readdir(rPath, (err, files) => {
if (err) throw err;
renameForFiles(rPath, files);
});