So I recently changed date format in the setting, and re-indexed the graph. But I noticed that in all old entries, my journal link is still in the old format ( [[Dec. 31st, 2024]] and not the new format [[2024_12_31]].
Is there a way to batch modify all my [[MMM. Do, YYYY]] to the new format?
I have asked chatgpt to help. THis is what it output. Would you mind quickly see if anything should be amended?
import os
import re
from datetime import datetime
# Define the function to convert the date format
def convert_date_format(match):
# Extract the date (e.g., 'Jan 2nd, 2025')
date_str = match.group(1)
# Remove suffixes like 'st', 'nd', 'rd', 'th' from the day part
date_str = re.sub(r'(\d+)(st|nd|rd|th)', r'\1', date_str)
# Parse the date into a datetime object (this will handle the comma)
try:
date_obj = datetime.strptime(date_str, '%b %d, %Y')
# Return the new format
return f"[[{date_obj.strftime('%Y_%m_%d')}]"
except ValueError:
# If there is an issue parsing the date, return the original match
return match.group(0)
# Directory containing the .md files
md_files_directory = "/path/to/your/markdown/files"
# Regular expression to find the dates in the old format
date_pattern = r'\[\[([A-Za-z]{3} \d{1,2}(?:st|nd|rd|th)?\, \d{4})\]\]'
# Walk through all files in the directory
for root, dirs, files in os.walk(md_files_directory):
for file in files:
if file.endswith(".md"):
file_path = os.path.join(root, file)
# Read the content of the markdown file
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Replace the old date format with the new one
new_content = re.sub(date_pattern, convert_date_format, content)
# If the content was modified, write the new content back to the file
if new_content != content:
with open(file_path, 'w', encoding='utf-8') as f:
f.write(new_content)
print(f"Updated: {file_path}")
I tried-and-errored the pytho. Below script works for me and I have successfully updated the graph. ps. I did not check all docs, but for the docs I sampled, nothing is changed except for the date format (which is the goal).
Update the .py for everyone here.
Note
update the graph path below.
my date format is from [[MMM Do, yyyy]] to [[yyyy_MM_DD]]. if your old entries uses different format, you need to ask chatgpt to change the code
it created a update log in the folder of where your .py is stored.
import os
import re
from datetime import datetime
# Define the function to convert the date format
def convert_date_format(match):
# Extract the date (e.g., 'Jan 2nd, 2025')
date_str = match.group(1)
# Remove suffixes like 'st', 'nd', 'rd', 'th' from the day part
date_str = re.sub(r'(\d+)(st|nd|rd|th)', r'\1', date_str)
# Ensure the date has no leading zeros in day and month
date_str = re.sub(r'\b0(\d)', r'\1', date_str)
# Parse the date into a datetime object (this will handle the comma)
try:
date_obj = datetime.strptime(date_str, '%b %d, %Y')
# Return the new format
return f"[[{date_obj.strftime('%Y_%m_%d')}]]"
except ValueError:
# If there is an issue parsing the date, return the original match
return match.group(0)
# Directory containing the .md files
md_files_directory = "C:/Users/XXX/YYY/OOO"
# Regular expression to find the dates in the old format
date_pattern = r'\[\[([A-Za-z]{3} \d{1,2}(?:st|nd|rd|th)?\, \d{4})\]\]'
# Open a log file to record updates
with open("update_log.txt", "w", encoding="utf-8") as log_file:
# Walk through all files in the directory
for root, dirs, files in os.walk(md_files_directory):
for file in files:
if file.endswith(".md"):
file_path = os.path.join(root, file)
# Read the content of the markdown file
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Replace the old date format with the new one
new_content = re.sub(date_pattern, convert_date_format, content)
# If the content was modified, write the new content back to the file
if new_content != content:
with open(file_path, 'w', encoding='utf-8') as f:
f.write(new_content)
# Log the update to the log file
log_file.write(f"Updated: {file_path}\n")
print("Date updates have been logged to 'update_log.txt'.")