Skip to content

Instantly share code, notes, and snippets.

@robert-kratz
Created December 24, 2023 00:29
Show Gist options
  • Save robert-kratz/40eea105ea86a33dfded737660e0d54e to your computer and use it in GitHub Desktop.
Save robert-kratz/40eea105ea86a33dfded737660e0d54e to your computer and use it in GitHub Desktop.
This script converts all images ending on .JPG to lowercase .jpg
const fs = require("fs");
const path = require("path");
const directoryPath = "./input"; // Replace with your directory path
fs.readdir(directoryPath, (err, files) => {
if (err) {
console.error("Error reading the directory", err);
return;
}
files.forEach((file) => {
if (path.extname(file).toLowerCase() === ".jpg") {
const oldPath = path.join(directoryPath, file);
const newPath = path.join(
directoryPath,
path.basename(file, ".JPG") + ".jpg"
);
fs.stat(oldPath, (err, stats) => {
if (err) {
console.error("Error getting file stats", err);
return;
}
const lastModified = stats.mtime;
fs.rename(oldPath, newPath, (err) => {
if (err) {
console.error("Error renaming file", err);
return;
}
console.log(`Successfully renamed ${oldPath} to ${newPath}`);
// Reset the last modification date
fs.utimes(newPath, new Date(), lastModified, (err) => {
if (err) {
console.error("Error setting file modification date", err);
}
});
});
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment