Dynamic link for last created note?

Not daily note. I wanna link last created note in specific folder.

If there is folder like this v

  • folder1
    • first note
    • second note
    • third note
    • last created note
  • folder2
    • main note

Then, in main note, I will make link for last create note. But it should change to the newer note when a new note is created in folder1

I searched for this feature for A MONTH but it didn’t help me.
Is it possible to implement this feature?

If in each of the notes you create under “folder 1”, you link to [[main note]], all these notes will automatically appear in the Backlinks, which you can sort so the last modified note appears on top.

You can achieve this using dataviewjs, like so:

```dataviewjs
const folder = "01 - Inbox";
const filesInFolder = app.vault.getMarkdownFiles().filter(file => file.path.startsWith(folder));
filesInFolder.sort((a, b) => a.stat.ctime < b.stat.ctime ? 1 : -1);
const latestFileName = filesInFolder[0].basename;
dv.paragraph(dv.fileLink(latestFileName));
```

If you want it inline, you can do it like so:

`$= const folder = "01 - Inbox"; const filesInFolder = app.vault.getMarkdownFiles().filter(file => file.path.startsWith(folder)); filesInFolder.sort((a, b) => a.stat.ctime < b.stat.ctime ? 1 : -1); const latestFileName = filesInFolder[0].basename; dv.span(dv.fileLink(latestFileName));`

Of course, changing “01 - Inbox” to the folder path you need.

I adapted this Templater snippet by @zachatoo for dataviewjs, basically just putting it in a dataviewjs codeblock and then displaying the link using dv.paragraph or dv.span combined with dv.fileLink.

Note, this is based on the ctime, which is not consistent across devices when you sync. For another solution based on the frontmatter, see the link above.

Hello,
that’s a nice thing.

How to get the date after the file…?

Lets say that it looks like this:
file_xyz 2024-03-14 15:00

Something like the following?

```
LIST file.ctime
FROM "Folder 1"
SORT file.ctime DESC 
LIMIT 1
```

Possibly with a dateformat()?

well, I was thinking more of the example above (@Fleralflora), not just print the file name (Link) but with asscociated ctime right behind…

This should work, with the creation date as part of the link’s display text / alias:

`$= const folder = "01 - Inbox"; const filesInFolder = app.vault.getMarkdownFiles().filter(file => file.path.startsWith(folder)); filesInFolder.sort((a, b) => a.stat.ctime < b.stat.ctime ? 1 : -1); const latestFile = filesInFolder[0]; const latestFileName = latestFile.basename; const ctime = moment(latestFile.stat.ctime).format("YYYY-MM-DD HH:mm"); dv.span(dv.fileLink(latestFileName, false, latestFileName + " " + ctime));`

Or in a more readable form:

```dataviewjs
const folder = "01 - Inbox"; 
const filesInFolder = app.vault.getMarkdownFiles().filter(file => file.path.startsWith(folder));
filesInFolder.sort((a, b) => a.stat.ctime < b.stat.ctime ? 1 : -1); 
const latestFile = filesInFolder[0];
const latestFileName = latestFile.basename; 
const ctime = moment(latestFile.stat.ctime).format("YYYY-MM-DD HH:mm");
dv.span(dv.fileLink(latestFileName, false, latestFileName + " " + ctime));
```

Only the inline example will integrate seamlessly with the text in your note.

1 Like

very nice… almost wonderful :wink:
Thank you very much!

You’re welcome! Glad I could help :slight_smile: This can be marked as solved then :+1:

although I’,m not the TO but as far as I’m concerned :man_shrugging:
:+1:

Ah, lost track of who was who.

It works INCREDIBLY well!!! Thank you for making my month worthwhile!!

1 Like

And here is yet an alternate version, which hopefully is slightly easier to read. First in the “long form”, and then as inline query script:

```dataviewjs
dv.span(dv.pages('"01 - Inbox"')
  .sort(p => p.file.ctime, 'desc')
  .map(p => p.file.link + " " + 
     p.file.ctime.toFormat("yyyy-MM-dd HH:mm"))
  .limit(1)[0])
```

This is more dataview centric and uses its function to get the result, and focusing on the markdown files. The output format should be the same, and the same in the inline form is shown below.

`$= dv.span(dv.pages('"01 - Inbox"').sort(p => p.file.ctime, 'desc').map(p => p.file.link + " " + p.file.ctime.toFormat("yyyy-MM-dd HH:mm")).limit(1)[0]) `

Update: Thanks to @Feralflora I discovered that my sorting was reversed, so I’ve now updated that…

2 Likes

Yeah, this seems like a better approach, in principle. However, it doesn’t give me the right result, so I suppose there’s some issue with the sort:
image

Was it a requirement to rebuild the link? That could also be easily done, but I understood it as they wanted the link with the creation time appended.

You could change the map though to something like: .map(p => dv.fileLink(p.file.link, "latest file " + p.file.ctime.toFormat("yyyy-MM-dd HH:mm")) to get a similar output if that specific text was wanted.

Other than that, it would of course help if I sorted the list correctly and use .sort(p => p.file.ctime, 'desc') instead of the default asc which it originally had! :smiley: (I’m now going to update my original post…)

No, I just thought it was easier to change the display text. I was just referring to how the note was not actually the latest one. Anyway, I see you fixed it :+1:

1 Like

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