Learning Obsidian - Automating deletion of file if same name exists in folder

Hey everyone!

I have decided to try and move my life from OneNote to Obsidian, my main use has been for journaling and I am tryingt to automate part of the process by creating a Journaling dashboard.

What I’m trying to do

I am using Templater and the buttons plugins.

I am trying to get something like this:

  1. Click the Button
  2. Create Template with name <%tp.date.now("Do - MMMM")%>
  3. Move it to the designated folder
  4. If the folders dont exist, create them based on Personal/Journal/Entries/Year/Month
  5. If a file exists with that name, delete the created file and open the existing file in the folder structure

Things I have tried

Below is where I am currently at! The file creates, and moves and creates folders.

I am currently stuck with switching to the existing document and deleting the other.

I am trying to avoid custom scripts in templater as I want it to work on iOS also.

I am not the best programmer but I am trying, any help would be appreciated!

Code in template:

const rootDir = `Personal/Journal/Entries/`;
const yearDir = `${rootDir}${tp.date.now("YYYY")}/`;
const monthDir = `${yearDir}${tp.date.now("MMMM")}/`;
const rootExists = this.app.vault.adapter.exists(rootDir, false);
const yearExists = this.app.vault.adapter.exists(yearDir, false);
const monthExists = this.app.vault.adapter.exists(monthDir, false);

Promise.all([rootExists, yearExists, monthExists]).then(
  async ([rootExists, yearExists, monthExists]) => {
    try {
      if (!rootExists) {
        await this.app.vault.createFolder(rootDir);
      }
      if (!yearExists) {
        await this.app.vault.createFolder(yearDir);
      }
      if (!monthExists) {
        await this.app.vault.createFolder(monthDir);
      }
    } catch (err) {
      console.log(err);
    }
  }
);

try {
  await tp.file.move(`${monthDir}${tp.file.title}`);
} catch (err) {
  console.log(err);
  const files = await app.vault.getMarkdownFiles();
  const activeEntry = files.filter(
    (file) => "/" + file.path === `${monthDir}${tp.file.title}.md`
  )[0];
  const thisFile = files.filter(
    (file) => "/" + file.path === `${tp.file.title}.md`
  )[0];
  
  this.app.vault.on('delete', (toDelete) => {

    this.app.workspace.activeLeaf.openFile(activeEntry);
  });

  await this.app.vault.delete(thisFile);
}

I have also tried some combination of the elements below as well.

I am assuming I need to get the code to run before to check if the file already exists rather than executing in the template but I am yet to figure out how to achieve that.

this.app.vault.on("create", async (thisFile) => {
  // your callback function here
  try {
    await tp.file.move(`${monthDir}${tp.file.title}`);
  } catch (err) {
    console.log(err);
    await this.app.vault.delete(toDelete);
  }
});


await this.app.vault.trash(thisFile).finally(() => {
  this.app.workspace.activeLeaf.openFile(files.filter((file) => + file.name === `Home.md`)[0]);
});

const home = files.filter((file) => +file.name === `Home.md`)[0];
await this.app.workspace.activeLeaf.openFile(home).then(() => {
  setTimeout(() => {
    this.app.vault.delete(thisFile), 1000
  })}).catch((err) => {
    console.log(err);
  });

figured it out using dataviewjs from this video below:

Community Talk on Buttons

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