Skip to content

Instantly share code, notes, and snippets.

@yuhangch
Created July 3, 2024 09:42
Show Gist options
  • Save yuhangch/aac6521029deaa30727ba8482813f3e0 to your computer and use it in GitHub Desktop.
Save yuhangch/aac6521029deaa30727ba8482813f3e0 to your computer and use it in GitHub Desktop.
Obsidian journals archived in YYYY/MM/YYYY-MM-DD format
// 90% writen by copilot
import fs from 'fs'
import path from 'path'
const ob_vault_path = "path/to/journals/"
const all_journal_files = fs.readdirSync(ob_vault_path).filter(file => file.endsWith('.md'))
all_journal_files.forEach(file => {
const date = file.split('.')[0]
const [year, month, day] = date.split('-')
if (!year || !month || !day) {
console.log(`Invalid date format for ${file}`)
return
}
const new_dir = path.join(ob_vault_path, year, month)
const old_file = path.join(ob_vault_path, file)
const new_file = path.join(new_dir, file)
if (!fs.existsSync(new_dir)) {
fs.mkdirSync(new_dir, {recursive: true})
}
fs.renameSync(old_file, new_file)
console.log(`Moved ${old_file} to ${new_file}`)
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment