Templater icon indicating copy to clipboard operation
Templater copied to clipboard

Add "relative" to tp.file.exists

Open Smitty010 opened this issue 2 years ago • 1 comments

The basic enhancement I'd like is to add a "relative" parameter to tp.file.exists(file :String, relative: boolean = false). The effect of the parameter is to treat the file as a full path (relative to the vault folder, like what you get back from tp.file.folder(true)

I left some comments in https://github.com/SilentVoid13/Templater/discussions/333#discussioncomment-1302141.

The basic issue is that app.vault.createFolder(path) expects a path relative to the vault base folder. Similarly, tp.file.move expects a path relative to the vault base folder. However, tp.file.exists expects a path relative to the current folder.

This is problematic in the case when you are creating a file with the template and you want to move it in place. So, long as the file is created in the vault base folder ("/"), you're fine. But, for example, periodic notes often get created in a non-root directory.

Additionally, app.vault.createFolder(path) seems to create as many subfolders as it needs to fill out the path (a good thing IMO). However, if all of the subfolder exist, then it aborts the script with an error.

If I was writing a script that took a file in one location and wanted to move it to an entirely different location in another set of subfolders (which may not exist yet), checking for existence would be problematic. The above change to tp.file.exists would make things very easy.

Smitty010 avatar Aug 22 '22 17:08 Smitty010

+1, would really be a nice feature. As discussed here:

For now I'm using (await tp.file.exists(tp.file.folder(false) + "/" + tp.date.now("YYYY-MM", 0) + ".md")) and for me this doesn't look pretty

ilandikov avatar Oct 03 '22 06:10 ilandikov