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…

3 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?

Thanks for the response.

<% tp.file.move( ‘People/1on1/’ + (mytitle = await tp.system.prompt( ‘Name:’, ‘test name’, false) )) %>

The above works. It moves the file to my desired folder location “People/1on1/” and prompts me to input the name of the file before creating.

<% tp.file.rename((tp.date.now() + ’ - ’ + (mytitle = await tp.system.prompt( ‘Meeting Name’, ‘test name’, false) ))) %>

The above works. It prompts me to input the name of the file before creating and puts today’s date before it in the file name.

What i’m trying to achieve
I would like to combine both of these functions and have not been able to get it to work thus far (my lack of knowledge).
What I would like to achieve when creating a new file from my template is to;

  • Be prompted to rename the file when creating, and
  • Automatically put in todays date in the file name/title, and
  • move the new file to a specific folder.
1 Like

How about

<% tp.file.move('People/1on1/' + tp.date.now() + '-' + (await tp.system.prompt('Name:','test name',false))) %>

?

Dave

2 Likes

Did not work, got the following: “Template parsing error, aborting”

Hmmm, something similar worked for me.

Couple thoughts:

  • Is your templater plug-in current?
  • you could add in mytitle like you have been using but I did not need it
<% tp.file.move('People/1on1/' + tp.date.now() + '-' + (await mytitle = tp.system.prompt('Name:','test name',false))) %>

My Obsidian is V0.14.15, Installer V0.14.6
My Templater is 1.12.0

2 Likes

It’s very strange, I tried it with mytitle (before you suggested) and it didn’t make a difference.

Good point with the versions;
My Obsidian is V0.14.15,
Installer V0.13.31 (when I check for updates it says underneath “your app is up-to-date”
My Templater is 1.12.0

1 Like

Does your developer console have any more information about the error? e.g. when I typo’d a variable name and got that error pop up, the console said “<typo’d variable name> not found”. To access the console: Ctrl-Shift-i on Windows/Linux or Cmd-Opt-I on macOS, and then go to the “Console” tab.

‘Bad template syntax’

Here is the full info from the console.

Templater Error: Template parsing error, aborting. 
 Bad template syntax

Unexpected token ')'
====================
var tR='',__l,__lP,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p,d){__l=p;__lP=d}
const _prs = [];
tR+='---\n'
_prs.push(tp.file.move( 'People/1on1/' + tp.data.now() + '-' (mytitle = await tp.system.prompt( 'Name:', 'test name', false) ))));
tR+='rJ2KqXzxQg'
tR+='\n\naliases: '
_prs.push(mytitle.trim());
tR+='rJ2KqXzxQg'
tR+='\n\ndate: '
_prs.push(tp.date.now());
tR+='rJ2KqXzxQg'
tR+='\n\ntime: '
_prs.push(tp.date.now("HH:MM"));
tR+='rJ2KqXzxQg'
tR+='\n\ntags: meeting, 1on1\n\n---\n\nAttendees:: [[@'
_prs.push(mytitle);
tR+='rJ2KqXzxQg'
tR+=']]\n\n'
const _rst = await Promise.all(_prs);
tR = tR.replace(/rJ2KqXzxQg/g, () => _rst.shift());
if(__l)tR=await includeFile(__l,Object.assign(tp,{body:tR},__lP))
if(cb){cb(null,tR)} return tR

log_error @ plugin:templater-obsidian:82

1 Like

I got it, there was a typo in it! Thank you all for the input and help

2 Likes

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