Skip to content

Instantly share code, notes, and snippets.

@Rankarusu
Last active December 22, 2024 17:04
Show Gist options
  • Save Rankarusu/29f7637a29c06176e7b0b5e440f76cbf to your computer and use it in GitHub Desktop.
Save Rankarusu/29f7637a29c06176e7b0b5e440f76cbf to your computer and use it in GitHub Desktop.
Update file modified date according to name
const { execSync } = require("child_process");
const fs = require("fs");
const pattern = /\D{3}_(\d{8})_.*/;
fs.readdir("./", (err, files) => {
files.forEach((file) => {
const match = pattern.exec(file);
const yyyymmdd = match?.[1];
if (yyyymmdd) {
const output = execSync(`touch -d ${yyyymmdd} -f ${file}`, { encoding: "utf-8" });
if (output) {
console.log(output);
return;
} else {
console.log(`Processed ${file}`);
}
} else {
console.log(`Skipped file ${file}`);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment