Trying to get a Templatr template to move new files to a folder based on front matter

What I’m trying to do

I have a Templatr template (below), which is for creating people I meet. It contains a front matter value of company. I’d like to use this front matter value to move the note to a subfolder with that front matter value.

---
company: 
location: 
title: 
email: 
website: 
aliases: 
---
tags:: [[People MOC]]

# <% tp.file.title %>
<% await tp.file.move("/Extras/People/" + tp.file.title) %>

## Notes
- 

Things I have tried

I have tried changing the move command to the below, but it only moves it to a folder called undefined, even after the company value is filled in.

<% await tp.file.move("/Extras/People/" + tp.frontmatter.company + "/" + tp.file.title) %>

I recon your issue is that templater is triggerred on creation of the file or when you ask it to create frontmatter at that time company is undefined hence not moving to the right folder,

A workaround is to have an template that only has templater commands in it and trigger it after you’ve filled in the front matter

I have a similar issue when I paste formated markdown from clipboard and wanted it it to pick up the title and move to a folder automatically however I think in that case there is a race condition and things happen to fast and are undefined when that part of the template triggers

Solved with the below template…

<%* 
const company = await tp.system.prompt("What company is this person a part of?");
await tp.file.move("/Extras/People/" + company + "/" + tp.file.title) 
%>
---
company: <%* tR += company; %>
location: 
title: 
email: 
website: 
aliases: 
---
`

## Notes
- 


4 Likes

And the yaml is valid after that? You don’t get an extra line in front of the first triple- dash?

Sometimes that could be an issue, which can be fixed by doing a _%> at the end.

1 Like

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