# Help refile to daily (and create if necessary)

**URL:** <https://org-roam.discourse.group/t/help-refile-to-daily-and-create-if-necessary/2602>\
**Category:** Requests\
**Created:** [May 13, 2022, 2:59pm UTC](https://org-roam.discourse.group/t/help-refile-to-daily-and-create-if-necessary/2602 "2022-05-13T14:59:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![japhir](https://avatars.discourse-cdn.com/v4/letter/j/b2d939/32.png) [@japhir](https://org-roam.discourse.group/u/japhir)\
**Post date:** [May 13, 2022, 2:59pm UTC](https://org-roam.discourse.group/t/help-refile-to-daily-and-create-if-necessary/2602/1 "2022-05-13T14:59:53Z")

</div>

Many times I capture stuff into my general [inbox.org](http://inbox.org) file. Some of those entries later turn out to be notes that I want to keep, but don’t need to be their own note. Thus I would like to move them to the correct daily entry file.

I would like to figure out how to:

- get the date of the current note from the CREATED property
- see if a daily note for that date exists
- if it does, refile it (preferably to the correct time between notes with already existing CREATED properties)
- if it doesn’t, capture a new daily file with this note’s contents (leaving the note’s created property intact)

Has anyone else found a need for such a workflow? And if so, would you be willing to share your elisp-fu? 🙂

---

<div class="post-metadata">

**Author:** ![telotortium](https://avatars.discourse-cdn.com/v4/letter/t/a587f6/32.png) [@telotortium](https://org-roam.discourse.group/u/telotortium)\
**Post date:** [May 14, 2022, 7:43am UTC](https://org-roam.discourse.group/t/help-refile-to-daily-and-create-if-necessary/2602/2 "2022-05-14T07:43:17Z")

</div>

Here’s some code I have that finds the daily file for a given date, and I think creates it if it doesn’t exist: [doom.d/org-config.el at a7d12591576dad86f0e2412f39abe915247a746f · telotortium/doom.d · GitHub](https://github.com/telotortium/doom.d/blob/a7d12591576dad86f0e2412f39abe915247a746f/org-config.el#L309)

I might have time to give a more detailed answer later.

---

<div class="post-metadata">

**Author:** ![russmatney](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/russmatney/32/351_2.png) [@russmatney](https://org-roam.discourse.group/u/russmatney)\
**Post date:** [May 19, 2022, 1:15pm UTC](https://org-roam.discourse.group/t/help-refile-to-daily-and-create-if-necessary/2602/3 "2022-05-19T13:15:15Z")

</div>

I have a similar workflow - it’s not perfect (note the `TODO` in there) and is a bit hard-coded (`~/todo` is my org and org-roam dir), but it works for refiling from point to today, tomorrow, yesterday, selecting a daily, or a few other common refile targets:

```auto
;; from https://emacs.stackexchange.com/questions/8045/org-refile-to-a-known-fixed-location
(defun russ/refile-to (file headline)
  "Move current headline to specified location"
  (let ((pos (save-excursion
               (find-file file)
               (org-find-exact-headline-in-buffer headline))))
    (org-refile nil nil (list headline file nil pos))
    (switch-to-buffer (current-buffer))))

(defun russ/refile-to-daily (n)
  ;; TODO this seems to sometimes nest the `daily/` an extra time :/
  (let ((file-s (format-time-string "~/todo/daily/%Y-%m-%d.org" (time-add (* n 86400) (current-time)))))
    (save-excursion
      (org-roam-dailies-capture-tomorrow n t)
      ;; TODO write the file?
      )
    (russ/refile-to file-s "new")))

(comment
 (format-time-string "%Y-%m-%d.org" (current-time))
 (format-time-string "%Y-%m-%d.org" (time-add 86400 (current-time)))
 (format-time-string "%Y-%m-%d.org" (time-add (* (- 1) 86400) (current-time))))

(defhydra hydra-org-refile-daily (:exit t)
  ;; TODO refile to today's daily note, create if it doesn't exist
  ("t" (russ/refile-to-daily 0) "Today")
  ("y" (russ/refile-to-daily -1) "Yesterday")
  ("T" (russ/refile-to-daily 1) "Tomorrow")
  ("j" russ/org-refile-to-daily-note "To some daily note" :column "Filter"))

(defhydra hydra-org-refile (:exit t)
  ("r" org-refile "Org refile" :column "~/todo")
  ("t" (russ/refile-to "~/todo/projects.org" "Todos") "project.org/Todos")
  ("h" (russ/refile-to "~/todo/projects.org" "Hammock") "project.org/Hammock")
  ("i" (russ/refile-to "~/todo/icebox.org" "new") "To icebox.org")
  ("p" (russ/refile-to "~/todo/principles.org" "new") "To principles.org")
  ("d" hydra-org-refile-daily/body "To some daily note"))

```

Source from my config if you’re digging: [dotfiles/+org-custom.el at master · russmatney/dotfiles · GitHub](https://github.com/russmatney/dotfiles/blob/master/emacs/.doom.d/%2Borg-custom.el#L193)
