Skip to content

Instantly share code, notes, and snippets.

@tieutantan
Created February 25, 2023 12:14
Show Gist options
  • Save tieutantan/81339dec52e8d30ff5c98ed882cbefed to your computer and use it in GitHub Desktop.
Save tieutantan/81339dec52e8d30ff5c98ed882cbefed to your computer and use it in GitHub Desktop.
Mass Update File on Windows
const fs = require('fs');
const path = require('path');
const directoryPaths = [
'./Photos from 2014',
'./Photos from 2015',
'./Photos from 2016',
'./Photos from 2017',
'./Photos from 2018',
'./Photos from 2019',
'./Photos from 2020',
'./Photos from 2021',
'./Photos from 2022',
'./Photos from 2023'
]; // list of directory paths
let count = 0;
directoryPaths.forEach(function(directoryPath) {
try {
const files = fs.readdirSync(directoryPath);
files.forEach(function(file) {
if (path.extname(file) === '.json') {
fs.unlinkSync(path.join(directoryPath, file));
console.log(`Deleted file ${path.join(directoryPath, file)}`);
count++;
}
});
} catch (err) {
console.error(`Error removing files from ${directoryPath}:`, err);
}
});
console.log(`Deleted a total of ${count} .json files`);
const fs = require('fs');
const path = require('path');
const directoryPaths = [
'./Photos from 2014',
'./Photos from 2015',
'./Photos from 2016',
'./Photos from 2017',
'./Photos from 2018',
'./Photos from 2019',
'./Photos from 2020',
'./Photos from 2021',
'./Photos from 2022',
'./Photos from 2023'
]; // list of directory paths
let photoCount = 0;
let videoCount = 0;
directoryPaths.forEach(function(directoryPath) {
try {
const yearMatch = directoryPath.match(/20\d{2}/);
let prefix = '';
if (yearMatch) {
prefix = yearMatch[0];
}
const files = fs.readdirSync(directoryPath);
files.forEach(function(file) {
const extname = path.extname(file).toLowerCase();
let count = 0;
let filePrefix = '';
if (extname === '.jpg' || extname === '.heic' || extname === '.jpeg' || extname === '.png') {
count = ++photoCount;
filePrefix = 'photo';
} else if (extname === '.mp4' || extname === '.3gp' || extname === '.avi' || extname === '.mov') {
count = ++videoCount;
filePrefix = 'video';
}
if (prefix) {
const oldPath = path.join(directoryPath, file);
const newName = `${prefix}-${filePrefix}-${count}${extname.toLowerCase()}`;
const newPath = path.join(directoryPath, newName);
fs.renameSync(oldPath, newPath);
console.log(`Renamed ${oldPath} to ${newPath}`);
}
});
} catch (err) {
console.error(`Error renaming files in ${directoryPath}:`, err);
}
});
console.log(`Renamed a total of ${photoCount} photos and ${videoCount} videos`);
const fs = require('fs');
const path = require('path');
const directoryPaths = [
'./Photos from 2014',
'./Photos from 2015',
'./Photos from 2016',
'./Photos from 2017',
'./Photos from 2018',
'./Photos from 2019',
'./Photos from 2020',
'./Photos from 2021',
'./Photos from 2022',
'./Photos from 2023'
]; // list of directory paths
let modifiedCount = 0;
directoryPaths.forEach(function(directoryPath) {
try {
const yearMatch = directoryPath.match(/20\d{2}/);
if (!yearMatch) {
console.warn(`Could not find year in directory path ${directoryPath}`);
return;
}
const year = yearMatch[0];
const files = fs.readdirSync(directoryPath);
files.forEach(function(file) {
const filePath = path.join(directoryPath, file);
const stats = fs.statSync(filePath);
const oldMtime = stats.mtime;
const newMtime = new Date(year, 0, 1); // set mtime to January 1st of the year
fs.utimesSync(filePath, newMtime, newMtime);
console.log(`Changed mtime of ${filePath} from ${oldMtime} to ${newMtime}`);
modifiedCount++;
});
} catch (err) {
console.error(`Error changing mtime of files in ${directoryPath}:`, err);
}
});
console.log(`Modified ${modifiedCount} files.`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment