Dynamic folder creation from a template

Things I have tried

I’ve spent a few hours on Google, YouTube, and on this forum but haven’t found a solution yet. I have tried to build this with the Templater plugin using tp.file.move and using the QuickAdd plugin, but neither do what I’m trying to do. More on both approaches later.

What I’m trying to do

I have a template I’m using for taking notes in meetings that I built in Templater called “MeetingTemplate”. When I create a new note using this template, I want it to automatically get filed in a folder based on the date the note was created.

For reference, I have something very similar set up and working with the Core Daily Notes plugin by putting the following text string in the Date format field: YYYY/MM-MMM/YYYY - MMDD - dddd

Things I have tried, Templater

Using Templater almost works like I want it to. Here’s my workflow:

  1. Open a new note with ⌘N
  2. Enter the name of the meeting as the note title.
  3. Invoke the command menu with ⌘P
  4. Search for “Templater: Insert Extras/Templates/MeetingTemplate.md”
  5. Hit enter and start taking notes.

And here’s the code for that template:

MeetingTemplate
---
date: <% tp.file.creation_date() %>
type: meeting
company: 
summary: " "
tags: notes/meeting
---
Index of all meeting notes: [[Map - Meetings]]

Date: [[<% tp.date.now("YYYY MMDD dddd") %>]]
<% await tp.file.move("/3. Meeting Notes/" + tp.date.now("YYYY MMDD") + " - " + tp.file.title) %>
# [[<% tp.date.now("YYYY MMDD - ") + " " + tp.file.title %>]]

**Attendees**: 
- <% tp.file.cursor() %>

## Agenda/Questions
- 

## Notes
-

This moves the note to my meeting notes folder “3. Meeting Notes” but I have a lot of meetings and I’d really like it to move it to a specific monthly folder like so: “3. Meeting Notes/YYYY/MM-MMM/”.

I’ve tried inserting various strings into the tp.file.move command but to no avail. Things I’ve tried, with new text in bold:

  • <% await tp.file.move(“/3. Meeting Notes/<% tp.date.now(YYYY) %>/<%tp.date.now(MM-MMM) %>” + tp.date.now(“YYYY MMDD”) + " - " + tp.file.title) %>
  • <% await tp.file.move(“/3. Meeting Notes/YYYY/MM-MMM/” + tp.date.now(“YYYY MMDD”) + " - " + tp.file.title) %>
  • <% await tp.file.move(“/3. Meeting Notes/{{DATE:YYYY}}/{{DATE:MM-MMM}}/” + tp.date.now(“YYYY MMDD”) + " - " + tp.file.title) %>

Things I have tried, Quick Add

Using Quick Add, I can get similar but not exactly the same results as with Templater. Here’s that workflow:

  1. Invoke the command menu with ⌘P
  2. Search for “Quick Add: Meeting”
  3. Enter meeting name in the modal
  4. Hit enter and start making notes

It’s using the same template file, so I’m going to just share the Quick Add settings page here:

I have tried the following things:

  • Setting the File Name Format to {{DATE:YYYY}}/{{DATE:MM-MM}}/{{NAME}} similar to how Daily Notes has it set, which is working.
  • Setting the Folder path to: 3. Meeting Notes/{{DATE:YYYY}}/{{DATE:MM-MM}}. That’s what is in there now, but it ignores the dynamic properties.

Neither of these seem to do the job. I’ve also noticed that when I use Quick Add, the <% tp.file.cursor() %> command doesn’t work and just shows up as plain text in the note.

Questions for the forum

  1. How would you solve this problem?

  2. Can Notes created using Quick Add set the cursor location for the newly drafted Note? I like to keep the keyboard as much as possible and this is a nice feature.

  3. What else should I be considering with this workflow?

2 Likes

Hi, I think my solution would be to change to a <%* javascript execution block, and build the new path within that code block. So I set out to try to do something like that, and ended up with the following template:

<%*
  const baseFolder = '/ForumStuff/f48198/'
  const minutes = tp.date.now('mm')
  const seconds = tp.date.now('ss')
  const newFolder = `${baseFolder}${minutes}/${seconds}/`
  console.log(baseFolder, minutes, seconds, newFolder)

  await tp.file.move(newFolder + tp.file.title)
%>

<% minutes + ":" + seconds %>

If you insert this into a file, it creates a new folder with the current minute, with the seconds as a sub folder of the minutes folder, and preserves the original file title. (Why you would want to do that with the minutes and second, that’s something entirely else :-D)

But it does work, and should be easily changed into using months and day of month, or whatever.

On the other topic, I would suggest looking into making a shortcut directly triggering your template and creating and moving the file directly from that shortcut. Then your entire process would be to hit your special meeting shortcut key, and it’ll create the file in the designated folder with a proper template ready to fill in the meeting details.

I think Templater has the option to set the file cursor, but I’ve not managed to get the working (and just recently discovered that maybe it’s because I’ve not set it up in the settings pane. Duh!)

Hope this helps,
Holroy

7 Likes

Holroy, this was really helpful and gave me what I needed to figure it out!

I wasn’t able to get your code to work on my end but I looked more into invoking javascript and found this really excellent article from Macdrifter: Obsidian Templater Fun.

Combining this article’s suggestions with yours about constructing a dynamic folder gives me something that seems to actually work even better than expected! The cursor position function seems to have broken again, but maybe that’s just how it’s going to be for now.

This is the relevant block, all written above the frontmatter:

<%*
let qcFileName = await tp.system.prompt("Note Title")
let titleName = tp.date.now("YYYY MMDD") + " - " + qcFileName
await tp.file.rename(titleName)
let baseFolder = "/3. Meeting Notes/"
let year = tp.date.now('YYYY') 
let month = tp.date.now('MM-MMM') 
let newFolder = `${baseFolder}${year}/${month}/` 
await tp.file.move(newFolder + titleName);
-%>
  • Note the “-” in the closing bracket which eliminates the empty line that appears there before the frontmatter if you don’t have it in there. That’s extra slick.

And for the sake of completion, this is a screenshot of the final template file:

7 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.