Problem with renaming folders in obsidian plugin

Hello everyone,
I am new to coding and I am exercizing by tring to create simple plugins for obsidian. The plugin I am trying to code creates a tree representations in specific folder in the folder note of the folder parent (I now the plugin already exist but I am only doing it for training).

What I’m trying to do

I struggle on a particular part of my plugin which consist in renamning a folderNote (note with the same name of his parent) when his parent folder is renamed.

My problem is that when a folder is renamed, all of his children (and children of children…) are also renamed so their path matches the new folder name. In my plugin I wish to wait for all of this files to be renamed before actually running a specific code to update the folderNote and them the tree representation.

Howerver the only way I found to solve that issue is by using timeOut wish I believe is not really suitable.

Here is my question

Are there beter and more “serious” ways to solve my problem ?

Things I have tried

I have tried to look into the code of similar plugins but they don’t work the same as mine and use flags to update the notes. So it does not resolve my problem.

Here is the part of the code of the main.ts :

async onload(): Promise<void> { () => {
		let isCallbackRunning = false; 
			// When a folder is renamed"rename", async (file, oldPath) => { 
				console.log(, " have been renamed.");
				console.log("callback :", isCallbackRunning);
				// We use a locker so there are no conflict with other files when a folder is being renamed 
				if (isCallbackRunning) {
					console.log("Callback already being executed");
				} else {
					// callback locker initialization
					isCallbackRunning = true;
					// We only want to deal with folder renaming
					if (file instanceof TFolder) {
						// We wait until all the children of the folder are renamed, 500ms seems to work
						setTimeout(() => {
							try {
								let oldFolderNotePath =
									file.path +
									"/" +
									basename(oldPath) +
								let folderNote =
								let newFolderNotePath =
									file.path + "/" + + ".md";
								console.log("folderNote :", folderNote);
								if (folderNote) {
									console.log("to be renamed");
									// We rename the folderNote so it matches the new parent name
									// We wait for the note to be renamed before making a new tree of the structure
									setTimeout(() => {
										// Appel de la fonction de mise à jour de l'arborescence
											"Updating tree srtucture...",
										isCallbackRunning = false;
									}, 500);

							} catch (error) {
						}, 500);

Thank very much,
Have a nice day !

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