Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added copy_from_file and copy_from_folder functions #1398

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

craigloewen-msft
Copy link

@craigloewen-msft craigloewen-msft commented Jun 17, 2024

This PR adds two functions: copy_from_file and copy_from_folder.

Goal

These functions allow users to take already existing files with full content and copy them to a new location. Some use case for this include: Maintain a record of your notes over time, Copy the latest to do list you have to a new folder so you can update its existing content easily, Copy latest status sync info, 'Template' with existing notes, Use the content you already have in an existing note and keep that up to date, copy new associated files and link to them in your templated note, and more!

Sample usage

Copy the latest modified file from one folder to another

<%* await tp.file.copy_from_folder("Status Sync", "lastModified","Status Sync") %>

Snapshot a particular file and link to it

Todo snapshot: [[<% (await tp.file.copy_from_file("Todos.md", "Snapshots", "Todo-" + tp.date.now("YYYY-MM-DD"))).basename %>]]

Improvements

This PR needs improvements for these areas. If the initial idea looks good please let me know and then I can finish coding in rest of the required items which I believe are:

  • Add documentation
  • Add test coverage
  • Would also want to add file matching (AKA use regex or wildcarding to match files with a specific pattern)

@craigloewen-msft
Copy link
Author

Can folks help review this? Pinging @Zachatoo !

If this functionality doesn't make sense to add in as general functionality I'm also happy to close the PR.

Thanks!

@Zachatoo
Copy link
Collaborator

Zachatoo commented Sep 2, 2024

Thank you for your interest in improving Templater!


Are there any significant differences between using tp.file.copy_from_file and a combination of tp.file.create_new and tp.file.include? Whether they are or not, I think it should be noted in the documentation. I am open to adding this as a new function.


In my opinion, copy_from_folder does not pass this criteria for submitting new internal functions. I haven't heard of other users having the use case you have of copying the latest file from one folder to another. If you open an issue and the issue gets a reasonable amount of user feedback that they want it built in, then I think we can proceed with adding this function.

Keep in mind that only pertinent submissions will be accepted, don't submit a very specific internal variable / function that you'll be the only one using.

@craigloewen-msft
Copy link
Author

Great question I actually took another stab at this and was able to get that copy_from_folder scenario working with Javascript. So I think we can close this if needed as my use case is satisfied, or you can add the functions as you like!

Thank you! :)

Here's the script I used incase it helps others:

### Notes
<%*
let fileStringMatch = 'Advanced Paste Weekly Sync';

const files = app.vault.getMarkdownFiles();
let latestFile = null;
let latestTime = 0;
console.log("Starting up", files);
for (const file of files) {
    if (file.basename.includes(fileStringMatch)) {
        console.log("Looking for this file");
        const stats = await app.vault.adapter.stat(file.path);
        if (stats.mtime > latestTime) {
            latestTime = stats.mtime;
            latestFile = file;
        }
    }
}

if (latestFile) {
    let content = await app.vault.read(latestFile);
    const lines = content.split('\n');
    const startIndex = lines.findIndex(line => line.trim() === '### Notes');
    if (startIndex !== -1) {
        const notesContent = lines.slice(startIndex + 1).join('\n');
        tR += notesContent;
    } else {
        tR += "No '### Notes' section found in the file.";
    }
} else {
    tR += "No file found with 'AdvancedPasteWeeklySync' in the title.";
}
%>
<% await tp.file.move("/General Meetings/" + tp.date.now("YYYY-MM-DD") + " Advanced Paste Weekly Sync") %>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants