Skip to content

Instantly share code, notes, and snippets.

@thrawn01
Created July 23, 2023 15:39
Show Gist options
  • Save thrawn01/719f66337402a4e6c9a410c55cf06c7b to your computer and use it in GitHub Desktop.
Save thrawn01/719f66337402a4e6c9a410c55cf06c7b to your computer and use it in GitHub Desktop.
Obsidian Script to Organize Daily Notes
/* This code organizes obsidian notes in format `YYYY-MM-DD.md` in the target directory into
the following folder structure `YYYY/MM Month/YYYY-MM-DD.md`
Examples
/2023/07 July/2023-07-26.md
/2023/08 August/2023-08-01.md
/2023/10 October/2023-10-31.md
*/
package main
import (
"fmt"
"os"
"path/filepath"
"time"
)
func organizeDailyNotes(directoryPath string) {
entries, err := os.ReadDir(directoryPath)
if err != nil {
fmt.Println("Error reading directory:", err)
return
}
for _, entry := range entries {
if entry.IsDir() {
continue // Skip directories
}
// Check if the file has a .md extension (daily note)
if filepath.Ext(entry.Name()) != ".md" {
continue
}
// Extract the date from the filename (YYYY-MM-DD.md)
dateStr := entry.Name()[:10]
date, err := time.Parse("2006-01-02", dateStr)
if err != nil {
fmt.Printf("Error parsing date from file %s: %s\n", entry.Name(), err)
continue
}
// Create the target folder path (YYYY/MM Month)
year := date.Year()
month := date.Month()
monthFolder := date.Format("January")
targetFolder := filepath.Join(directoryPath, fmt.Sprintf("%d/%02d %s", year, month, monthFolder))
// Create the target folder if it doesn't exist
if err := os.MkdirAll(targetFolder, 0755); err != nil {
fmt.Printf("Error creating folder %s: %s\n", targetFolder, err)
continue
}
// Move the file to the target folder
srcFile := filepath.Join(directoryPath, entry.Name())
targetFile := filepath.Join(targetFolder, entry.Name())
if err := os.Rename(srcFile, targetFile); err != nil {
fmt.Printf("Error moving file %s to %s: %s\n", srcFile, targetFile, err)
continue
}
fmt.Printf("Moved %s to %s\n", srcFile, targetFile)
}
}
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: go run main.go <directory_path>")
return
}
organizeDailyNotes(os.Args[1])
}
@b3nz3n3
Copy link

b3nz3n3 commented Dec 3, 2023

Thank you for this script. It was very helpful to organize my journal entries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment