Dynamically Log Files creation in Daily Logs Help!

Dynamically Log Files creation in Daily Logs

What I want to do:

  • I want to be able to know what files were created on which date, located in my daily notes.
  • My daily notes has a logs section where I would like to log stuff I have done in that day.
  • When I create a file I would like it to be logged automatically upon file creation so I don’t have to do it manually.

What I though about doing:

  • Well I was thinking that it would be done by using templater plugin… but I initially could not see an option to do that
  • Dataview does solve this problem one way… but because its in dataview… i can not view the file in graph view or use mmy backlinks to see the file linked in the file

Dataview solution

```dataview
TABLE WITHOUT ID
file.link as Files, file.cday as Create, file.mday as Modified 
FROM ""
WHERE file.cday = date(today)
```

Final Thoughts

  • I am currently manually inputting the links my self, because i would rather see the links in my graphview, but I would like to know if either there is a way to make dataview links appear in the graphview or backlinks, or if there is a way to use templater to insert the files i have created into the logs section of my daily notes.

You can use that query within a dataviewjs query, called from a template, to insert it as static data using Templater’s execution command blocks. That’s a handful of strangeness, right?

Let’s give an example, which might be close to what you want. I’m assuming that your daily notes are named using the format YYYY-MM-DD, and that you insert this template after today… :slight_smile:

<%*

const files = await dv.query(`
TABLE WITHOUT ID
file.link as Files, file.cday as Create, file.mday as Modified 
WHERE file.cday = date(this.file.name)
`)

if ( files.successful )  {
  tR += dv.markdownTable(files.value.headers, files.value.values)
}
_%>

This would create the static version of that query, so for example the modified time will not ever change again. So maybe, it’s an idea to just get rid of that field from the table.

Code untested, but you get the gist of the idea, hopefully.


Thankyou for replying so soon! I did attempt to use the dataview method of inputting the file in, sadly I was not able to find much resources online to help me with that method… I tied fiddling with the code you gave me too… but that didn’t work out for me either…

My Current solution

  • Searching the web, I cam across this website:
  • Logging Templater Example - shabeblog
  • It gave me some code that… at first had a lot of bugs, maybe due to updates in both the templater plugin updating or obsidian updating. But I managed to get some output from it that could help

The Original Code

<%*
	const file = tp.file.find_tfile(tp.date.now("YYYY-MM-DD"))
	if (file) {
		const loggedItem = await tp.system.prompt("What's Up?")
		const time = tp.date.now("HH:mm")
		const content = await app.vault.read(file).split("\n")
		const index = content.indexOf("### Log")
		content.splice(index + 1, , `- ${time} - ${loggedItem}`)
		await app.vault.modify(file, content.join("\n"))
	} 
	else {
		new Notification("No Daily Note Found!")
	}
-%>
  • This had a big problem cause most of it looks off when you put it in your own file
  • fixing the code by adding a numeral value (i just put a zero) in the splice does clean the view to make it look like it works but it does not run

My Modified code

<%*
// Get the tfile value for the daily note based on year month day format 
const file = tp.file.find_tfile(tp.date.now("YYYY-MM-DD"))

if (file) {
	// Gets data from file stores it contentArray
	const content = await app.vault.read(file)
	// Split for some reason wont split in the code above
	const contentArray = content.split("\n");

	// Get the tile for link
	const p = tp.file.title
	
	// Text to be inputted in the file
	const stuff = `- `+ tp.date.now("HH:mm") + ` Created \[\[${p}\]\]`

	// I want the text to go at the bottom of the list of things i log
	// but I dont have a section after logs so this works for now
	contentArray.push(`${stuff}`)
	// Modifies the file at the end
	await app.vault.modify(file, contentArray.join("\n"))
} 
else {
	new Notification("No Daily Note Found!")
}
-%> 

Other Modifications

  • there is a modifcation to the code people can make if logs is not the at the bottom of your code list, but it adds new logged items above the previously logged item
    Replace:
	// I want the text to go at the bottom of the list of things i log
	// but I dont have a section after logs so this works for now
	contentArray.push(`${stuff}`)

With This:

	const index = contentArray.indexOf("## Log")
	contentArray.splice(index + 1, 0, `${stuff}`)
  • Change ## Logs to whatever heading you want to input this after

Final Thoughts

  • My goal was to dynamically add my created files to my logs of my daily notes, in the way such that when I create a file it would be logged into my logs section which is current located at the bottom of my daily notes.
  • I believe the current solution I have does the trick, but I feel its not a permanent solution cause:
    • 1 It does not account for files being deleted
    • 2 I have not tested the boundary cases
    • 3 updates to templater could break this again
  • I’m leaving this here if anyone could improve on the code I created, please do post improvement you guys make and other methods to accomplish this!

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