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?
Howdy, Stranger!
Categories
- 3K All Categories
- 152 Research & Reading
- 692 The Zettelkasten Method
- 7 Knowledge Work
- 100 Writing
- 464 Software & Gadgets
- 154 Workflows
- 730 The Archive
- 15 Plug-In Showcase
- 88 Resolved Issues
- 225 Projects Logs and Journals
- 83 Project: Zettelkasten.de
- 53 Critique my Zettel
- 171 Random
- 373 Introduce Yourselves!
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
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.
My Internet Home — My Now Page
@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
/[[(\d{12})]]/202312061552corresponds to the filename202312061552 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.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/