Create a note and place that note into a new folder with the same name as the note

What I’m trying to do

I want to create a note, and right now I get a prompt to give the file a name.
But I also want to create a folder with that same filename and move that note into the folder.

Things I have tried

I have searched this forum.

I suggest taking a look at the QuickAdd plugin. It can do this and much, much more. It’s great!

What are you using to get that prompt? Use Create new note from a context meny? Are you using some template engine to get it?

What are you using to get that prompt? Use Create new note from a context meny? Are you using some template engine to get it?

I have a new note template with this.
But this applies for ALL notes, I only need this feature for a specific note type:

<%*
  let title = tp.file.title
  if (title.startsWith("Untitled")) {
    title = await tp.system.prompt("Title");
    await tp.file.rename(title);
  } 
  tR += "---"
%>
title: <%* tR += title %>
creation_date: <% tp.date.now("yyyy-MM-DD") %> 
modification_date: <% tp.file.last_modified_date("YYYY-MM-DD") %> 
tags: 
---

I suggest taking a look at the QuickAdd plugin. It can do this and much, much more. It’s great!

I have but this does not do what I want … It can only create a note in a folder, but not name the folder the same as the note when creating the note, and putting that note in the folder.

Extend that template so that if your name matches whatever classifies it to have its own folder, and then create that folder and move the file.

How do you determine the file type? Is it a name prefix, or suffix? Is it done after you’ve created the new file? (If the latter you’ll need to trigger another template as the new file template is airway done)

Then I have misunderstood what you’re trying to do. It is simple to have QuickAdd create a new file in a folder whose name matches the file name. I use this ability often.

I guess what you want is something different.

Thanks,

I managed to change the existing code with an if statement and IF the note name starts with a specific prefix I am creating a net folder and placing that note in the folder with the same name.

The final script:

<%*
  let title = tp.file.title;

  // Handle "Untitled" notes by prompting for a title
  if (title.startsWith("Untitled")) {
    title = await tp.system.prompt("Title");
    await tp.file.rename(title);
  } 

  // Handle FOLDER notes by creating a folder and moving the note into it
  if (title.startsWith("FOLDER")) {
    let basePath = "FOLDER-NOTES/";
    let folderPath = basePath + title; // Folder path named after the note
    let newFilePath = folderPath + "/" + title + ".md"; // Note inside the folder

    // Workaround: Create an empty file in the folder to ensure it exists
    await app.vault.createFolder(folderPath).catch(() => {});

    // Move the note into the new folder
    await tp.file.move(newFilePath);
  }

  tR += "---";
%>
title: <%* tR += title %>
creation_date: <% tp.date.now("yyyy-MM-DD") %> 
modification_date: <% tp.file.last_modified_date("YYYY-MM-DD") %> 
tags: 
---