Created
December 24, 2023 00:29
-
-
Save robert-kratz/40eea105ea86a33dfded737660e0d54e to your computer and use it in GitHub Desktop.
This script converts all images ending on .JPG to lowercase .jpg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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