# Templater script to create folder-file pair not working?

**URL:** <https://forum.obsidian.md/t/templater-script-to-create-folder-file-pair-not-working/76960>\
**Category:** Help\
**Tags:** templater\
**Created:** [February 15, 2024, 12:18pm UTC](https://forum.obsidian.md/t/templater-script-to-create-folder-file-pair-not-working/76960 "2024-02-15T12:18:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![maddyboy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/maddyboy/32/18284_2.png) [@maddyboy](https://forum.obsidian.md/u/maddyboy)\
**Post date:** [February 15, 2024, 12:18pm UTC](https://forum.obsidian.md/t/templater-script-to-create-folder-file-pair-not-working/76960/1 "2024-02-15T12:18:35Z")

</div>

Hello,

**I want to run a dataview script for new note. First, it should ask for a prompt for giving the note title, then it should create a new directory (by that title), then create an `"index.md"` file inside it.**

I have written templater code it is not working. Whats wrong ?

```auto
<% let title = await tp.system.prompt("Enter Blog Post Title:")

// Handle blank titles 
if (title === "") {
  tp.system.showMessage("Please enter a valid title.", { type: "error" });
  return;
}

// Construct folder path 
const blogFolder = "/content/post/all_posts"; 

// Check if folder exists 
if (!await tp.file.exists(blogFolder)) {
  tp.system.showMessage("The specified folder path doesn't exist.", { type: "error" });
  return;
}

// Create directory 
const dirPath = `${blogFolder}/${title}`;
if (!await tp.file.exists(dirPath)) {
  await tp.file.createFolder(dirPath);
}

// Create index.md file and populate basic content
const filePath = `${dirPath}/index.md`;
await tp.file.create(filePath, `---
title: ${title}
date: ${tp.date.now("YYYY-MM-DD")}
tags: []
---

Write your blog post content here.
`);

tp.file.open(filePath);
%>

```

---

<div class="post-metadata">

**Author:** ![Anwen](https://forum.obsidian.md/letter_avatar_proxy/v4/letter/a/b5e925/32.png) [@Anwen](https://forum.obsidian.md/u/Anwen)\
**Post date:** [February 16, 2024, 1:06am UTC](https://forum.obsidian.md/t/templater-script-to-create-folder-file-pair-not-working/76960/2 "2024-02-16T01:06:16Z")

</div>

When you try a template and it doesn’t work, you can open the console with Ctrl+Shift+I, and look at the error message in the console. Here, a parenthesis is missing.  
Well, that’s a bit of a stretch.  
The first problem is that for templater codes such as these you have to use `<%* %>` and not `<% %>`.  
Secondly, the tp.system.showMessage function doesn’t exist. Having looked into what the obsidian api had to offer, I think it’s better to use `tp.obsidian.notice()`.  
Finally, the tp.file.createFolder function doesn’t exist either. I haven’t found a way yet, but you’ll have to do things differently.  
This may be possible with the Obsidian api (tp.obsidian.FileManager.prototype.createNewFolder comes close, but I haven’t yet found out if it’s a usable function and how to use it.  
There’s also an error in the functions tp.file.create (tp.file.create\_new) and tp.file.open doesn’t exist either.

So, the part that creates the folder still needs to be made functional, but this code comes pretty close:

```auto
<%*
let title = await tp.system.prompt("Enter Blog Post Title:");

// Handle blank titles 
if (title === "") {
  tp.obsidian.Notice("Please enter a valid title.");
  return;
}

// Construct folder path 
const blogFolder = "/content/post/all_posts"; 

// Check if folder exists 
if (!await tp.file.exists(blogFolder)) {
  tp.obsidian.Notice("The specified folder path doesn't exist.");
  return;
}

// Create directory 
const dirPath = `${blogFolder}/${title}`;
if (!await tp.file.exists(dirPath)) {
 // await tp.obsidian.??;
}

// Create index.md file and populate basic content
const filePath = `${dirPath}/index.md`;
await tp.file.create_new(filePath, `---
title: ${title}
date: ${tp.date.now("YYYY-MM-DD")}
tags: []
---

Write your blog post content here.
`);

// tp.file.open(filePath);
%>

```

---

<div class="post-metadata">

**Author:** ![maddyboy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/maddyboy/32/18284_2.png) [@maddyboy](https://forum.obsidian.md/u/maddyboy)\
**Post date:** [February 16, 2024, 7:25am UTC](https://forum.obsidian.md/t/templater-script-to-create-folder-file-pair-not-working/76960/3 "2024-02-16T07:25:24Z")

</div>

thank you. I will try. 🙂

---

<div class="post-metadata">

**Author:** ![gino\_m](https://forum.obsidian.md/letter_avatar_proxy/v4/letter/g/3ec8ea/32.png) [@gino\_m](https://forum.obsidian.md/u/gino_m)\
**Post date:** [February 16, 2024, 10:39am UTC](https://forum.obsidian.md/t/templater-script-to-create-folder-file-pair-not-working/76960/4 "2024-02-16T10:39:01Z")

</div>

> [@Anwen](#):
>
> Finally, the tp.file.createFolder function doesn’t exist either. I haven’t found a way yet, but you’ll have to do things differently.  
> This may be possible with the Obsidian api (tp.obsidian.FileManager.prototype.createNewFolder comes close, but I haven’t yet found out if it’s a usable function and how to use it.

Just chirping in about folder creation.  
Perusing the [Templater snippets kindly shared by Zachatoo](https://zachyoung.dev/posts/templater-snippets), I seem to have found the solution to be `tp.file.move` as used in [this](https://forum.obsidian.md/t/dynamic-folder-creation-from-a-template/48198/2) thread/post, but I’d be surprised if that’s the only way. Not being a coder – I’d still say there must be another way that doesn’t require the `fs` module.  
Maybe someone can offer the Obsidian API solution.

---

<div class="post-metadata">

**Author:** ![maddyboy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/maddyboy/32/18284_2.png) [@maddyboy](https://forum.obsidian.md/u/maddyboy)\
**Post date:** [February 17, 2024, 5:54am UTC](https://forum.obsidian.md/t/templater-script-to-create-folder-file-pair-not-working/76960/5 "2024-02-17T05:54:45Z")

</div>

Thanks [gino\_m](https://forum.obsidian.md/t/templater-script-to-create-folder-file-pair-not-working/76960/4). Fabulous 😄. I added it to periodic notes daily template. I click the button and it prompts. Working.

```auto
<%*
let qcFileName = await tp.system.prompt("Note Title")
let titleName = qcFileName
await tp.file.rename('index')
let baseFolder = "/content/post/"
let newFolder = `${baseFolder}/${titleName}/` 
await tp.file.move(newFolder + `index`);
let title = titleName
let description = ""
let slug = ""
let date = ""
let image = ""
let categories = ""
let tags = ""
let weight = ""
setTimeout(() => { app.fileManager.processFrontMatter(tp.config.target_file, frontmatter =>{ frontmatter["title"] = title; 
frontmatter["description"] = description; 
frontmatter["slug"] = slug; 
frontmatter["date"] = date; 
frontmatter["image"] = image; 
frontmatter["categories"] = categories; 
frontmatter["tags"] = tags; 
frontmatter["weight"] = 1; 
}) }, 200)
-%>

```

---

<div class="post-metadata">

**Author:** ![system](https://forum.obsidian.md/uploads/default/original/3X/b/e/bec35fbd23ab28909bb6bfe62cb32eb82dd34019.png) [@system](https://forum.obsidian.md/u/system)\
**Post date:** [February 24, 2024, 5:55am UTC](https://forum.obsidian.md/t/templater-script-to-create-folder-file-pair-not-working/76960/6 "2024-02-24T05:55:31Z")

</div>

Closed on February 24, 2024, 5:55am UTC

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