Skip to content

Instantly share code, notes, and snippets.

@helderberto
Created June 12, 2023 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helderberto/cf21dc6eb2f9b713183aae9ce1237c1e to your computer and use it in GitHub Desktop.
Save helderberto/cf21dc6eb2f9b713183aae9ce1237c1e to your computer and use it in GitHub Desktop.
YYYY_MM_DD.md to YYYY-MM-DD.md
const fs = require('fs');
const path = require('path');
const targetDir = './your_folder'; // Replace with your target folder
// Regular expression pattern to match files by YYYY_MM_DD.md format
const filePattern = /^\d{4}_\d{2}_\d{2}\.md$/;
// Get a list of all files in the target folder
fs.readdir(targetDir, (err, files) => {
if (err) {
console.error('Error reading target folder:', err);
return;
}
// Iterate through the files in the target folder
files.forEach((file) => {
if (filePattern.test(file)) {
const sourcePath = path.join(targetDir, file);
const targetFile = file.replace(filePattern, (match) => {
const datePart = match.split('.')[0];
const parts = datePart.split('_');
return `${parts[0]}-${parts[1]}-${parts[2]}.md`;
});
const targetPath = path.join(targetDir, targetFile);
// Rename the file
fs.rename(sourcePath, targetPath, (err) => {
if (err) {
console.error(`Error renaming file ${file}:`, err);
return;
}
console.log(`File ${file} transformed to ${targetFile}`);
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment