Properties Wrangler: Add a way to "Insert", "Rename" and "Remove" properties and values in all files

Thank you Leifip.

Yes, the script you provided writes/replaces all files without testing if the property was used or not. (line “with open(file_path, ‘w’) as file:” and th next one)

I have used ChatGPT first to ensure it is possible to modify a file so out of Obsidian…
Its answer is yes :

es, Obsidian typically picks up changes made to the files within your vault, even if you make modifications manually outside of the Obsidian application. Obsidian relies on the file system to detect changes in your vault directory.

When you modify a file, including changing a property name, outside of Obsidian, the next time you launch Obsidian, it should recognize the changes and update its internal representation of the vault accordingly. Obsidian scans the files in your vault directory during startup and loads any new or modified files.

Then I’ve asked it to produce a python script modifying only the relevant files (not tested, I provided it as is)

(The correction point is in the added test “if updated_content != content:”)

import os
import re

def rename_property(file_path, old_property, new_property):
    with open(file_path, 'r', encoding='utf-8') as file:
        content = file.read()

    # Use a regular expression to find and replace the property name
    updated_content = re.sub(fr'(?<=^|\n){old_property}:', f'{new_property}:', content)

    # Check if the content was actually changed
    if updated_content != content:
        # Write the updated content back to the file
        with open(file_path, 'w', encoding='utf-8') as file:
            file.write(updated_content)

def rename_property_in_vault(vault_path, old_property, new_property):
    # Walk through all files in the vault
    for foldername, subfolders, filenames in os.walk(vault_path):
        for filename in filenames:
            if filename.endswith('.md'):  # Process only Markdown files
                file_path = os.path.join(foldername, filename)

                # Check if the property exists in the file before attempting to rename
                with open(file_path, 'r', encoding='utf-8') as file:
                    content = file.read()
                    if re.search(fr'(?<=^|\n){old_property}:', content):
                        rename_property(file_path, old_property, new_property)

if __name__ == "__main__":
    # Replace these values with your actual property names
    old_property_name = "old_property"
    new_property_name = "new_property"

    # Replace this with the path to your Obsidian vault
    obsidian_vault_path = "/path/to/your/obsidian/vault"

    # Rename the property in files that contain the property
    rename_property_in_vault(obsidian_vault_path, old_property_name, new_property_name)

Notice :

Adjust the old_property_name, new_property_name, and obsidian_vault_path variables as needed.

Please note that this script assumes that your properties are formatted as property_name: at the beginning of a line or after a newline character. If your property format is different, you may need to modify the regular expression accordingly. Also, always make a backup of your vault before running scripts that modify multiple files.

1 Like