Zettelkasten Forum


Possible to expand UIDs to full title in wikilinks?

Is it possible to expand all UIDs, in wikilinks in my notes to their full file names?

Comments

  • I'm curious why you are considering expanding the links to include their full file names. What if a filename changes? Of course, this assumes that a note's title is also its filename, and you might want to change the title on a future review or refactoring.

    Will Simpson
    I must keep doing my best even though I'm a failure. My peak cognition is behind me. One day soon I will read my last book, write my last note, eat my last meal, and kiss my sweetie for the last time.
    kestrelcreek.com

  • @DavidWJ It's not built into The Archive, so there's no easy way, no.

    It's possible to approach this with an automation tool or script.

    ⚠️ Make a backup (e.g. Zip/compress the folder) first!

    The following is almost a blueprint that can make ChatGPT generate a script for you :)

    1. Assuming that all UIDs are date-time stamps of 12 digits,
    2. Collect all filename in your notes folder.
    3. Transform the filenames collection into a hashmap aka dictionary of UID-to-filename. Check that all file UIDs are unique in the process. Report (print) which are used multiple times, and abort.
    4. For each file from the collection:
      1. Read its content (read-write mode) to replace all wiki links. Wiki links are denoted by UIDs enclosed in double brackets, which is represented by this regular expression: /[[(\d{12})]]/
      2. For each matched wiki link:
        1. Replace the matched UID with the full filename. For example, if the UID 202312061552 corresponds to the filename 202312061552 My best note ever.txt, replace the match with "202312061552 My best note ever.txt". The prepared dictionary of UID-to-filename pays off here.
      3. Replace the file contents with the replaced version.

    I went ahead and asked ChatGPT with some instructions:
    https://chat.openai.com/share/7609ad5c-8c4b-477f-b3e8-48e3f554eca6

    Untested Python script output in case the link breaks and someone wants to refine this:

    import os
    import re
    import sys
    
    def collect_filenames(directory):
        filenames = {}
        duplicate_uids = []
        for filename in os.listdir(directory):
            if os.path.isfile(os.path.join(directory, filename)):
                uid_match = re.search(r'(\d{12})', filename)
                if uid_match:
                    uid = uid_match.group(0)
                    if uid in filenames:
                        duplicate_uids.append(uid)
                    filenames[uid] = filename
        if duplicate_uids:
            print(f"Error: Multiple files found for the following UIDs: {', '.join(duplicate_uids)}")
            sys.exit(1)
        return filenames
    
    def replace_wiki_links(directory, filenames):
        wiki_link_pattern = re.compile(r'\[\[(\d{12})\]\]')
        for filename, file_uid in filenames.items():
            file_path = os.path.join(directory, filename)
            with open(file_path, 'r') as file:
                content = file.read()
                replaced_content = re.sub(wiki_link_pattern, lambda match: filenames.get(match.group(1), match.group(0)), content)
            with open(file_path, 'w') as file:
                file.write(replaced_content)
    
    def main(directory):
        filenames = collect_filenames(directory)
        replace_wiki_links(directory, filenames)
    
    if __name__ == "__main__":
        if len(sys.argv) != 2:
            print("Usage: python script.py <directory_path>")
            sys.exit(1)
        directory_path = sys.argv[1]
        if not os.path.isdir(directory_path):
            print("Error: Invalid directory path")
            sys.exit(1)
        main(directory_path)
    

    Author at Zettelkasten.de • https://christiantietze.de/

Sign In or Register to comment.