Increment file name by 1 with templater

What I’m trying to do

Hi, I’m trying to let templater increment the files name by 1 if it already exists in a folder. Usually i would just let this be done by Quickadd but it doesn’t work, because the new notes creation folder is not the same as its storing folder. (I do this so it can automatically link to the project this meeting is associated to.) Quicknote generates the files name before acutally creating the file, so it logically does not check the final folder for dublicates.

Things I have tried

I have looked online, but did not find any solution. I tried to make a script but that doesn’t seem to work.

<%*
const meetingsFolder = 'Meetings/';
const today = tp.file.date('YYYY-MM-DD');

let meetingNumber = 1;
let meetingName = `Meeting ${today} No. ${meetingNumber}`;

while (tp.file.exists(meetingsFolder + meetingName)) {
	meetingNumber++;
	meetingName = `Meeting ${today} No. ${meetingNumber}`;
}
tp.file.rename(meetingName)
%>

When trying to run this template Obsidian just freezes and I am forced to close the program.

What is my goal?

  1. Click a Button on the projects page
  2. Open a new note + automatic name added via quickadd (Meeting + date + No.1) (still in the project folder)
  3. execute the template commands
  4. check the “Meetings/” folder rename the note if the date and No. already exists by incrementing the No. by 1.
  5. move the newly renamed file to “Meetings/”

I have slightly modified the code:

<%*
const meetingsFolder = 'Meetings/';
const today = tp.date.now('YYYY-MM-DD');

let meetingNumber = 1;
let meetingName = `Meeting ${today} No. ${meetingNumber}`;

if (tp.file.exists('Meetings/Meeting 2023-09-06 No. 1')) {
	meetingNumber ++;
	meetingName = `Meeting ${today} No. ${meetingNumber}`;
}
await tp.file.rename(meetingName)
%>

I changed the while to an if statement and gave a direct link to the file. This defeats the purpous, but it seems that the while statement is causing the problem.

Edit: when using the while statement it still crashes the program, even when directly linking to the note it should compare.

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