Templater: Rename a file and move it to a folder

What I’m trying to do

I’m creating my own Zettlekasten-style template (I realise there is a default plugin for this but I want a little more customisation of my templates) using the Templater community plugin.

On applying the template to a file I want to:

  1. Automatically set the title to be that of the current timestamp
  2. Move the file to a ‘Notes’ folder (with the new title)

What I’ve tried

I’ve tried using this in my templates but am currently receiving a “template parsing error”.

<%*  
	// Set the current timestamp as the filename
	await tp.file.rename(tp.file.creation_date("YYYYMMDDHHmmss"));
	  await title = tp.file.title;

	// Move to the Notes folder
		tp.file.move("Notes/" + title)
%>

Can anyone help to suggest how I may fix the issue and achieve the above two steps?

Thanks.

1 Like

Try it in one step (my date format is different):
<% await tp.file.move(“Notes” + “/” + tp.file.creation_date(“YY[-]MM[-]DD”)) %>

While I’m not an expert, seems to work for me.

3 Likes

Thanks for the suggestion. I modified the script to:

<% tp.file.move("Notes/" + tp.file.creation_date("YYYYMMDDHHmmss")) %>

But receive this error:

ENOENT: no such file or directory, rename '/path/to/Obsidian/Untitled.md' -> '/path/to/Obsidian/Notes/20220621162312.md'

Maybe it’s having problems renaming and moving at the same time?

2 Likes

Ah, the problem was actually to do with paths as I made a small typo. I can confirm that your suggestion works perfectly, and cuts out an extra rename operation.

Thanks @Twita !

1 Like

Excellent !
I’ve received tons of help from others so thrilled I can start to give back…

2 Likes

Hi, I am looking at a similar use case and having trouble.

I have a template that sets the date and persons name (Im prompted to input this),

<% tp.file.rename((tp.date.now("YYYY-MMM-DD") + ' - ' + (mytitle = await tp.system.prompt( 'Meeting Name', 'test name', false) ))) %>

but I also want to automatically move this file to a specific folder (“1on1”).

<% await tp.file.move("/1on1") %>

I have tried a few variants but don’t seem to be able to use both functions together. I tried the following but get template parsing error, aborting”.

<% await tp.file.move("/1on1" + tp.file.rename((tp.date.now("YYYY-MMM-DD") + ' - ' + (mytitle = await tp.system.prompt( 'Meeting Name', 'test name', false) )))) %>

and

<% await tp.file.move("/1on1" + "/" + tp.file.creation_date("YYYY-MMM-DD") + ' - ' + (mytitle = await tp.system.prompt( 'Meeting Name', 'test name', false) ))) %>

Anyone any ideas?

Yes, you do want a “/” between your folder name and your file name. Were your rename and move lines working independently? (i.e. if you tried just one of them)

I’m a little surprised that setting a variable in the middle of your call to rename (now move) is working. Do you use mytitle elsewhere? If not, can you get rid of just mytitle = from the line I quoted? Does that change anything?