Forked from jenningsb2/roam-obsidian-date-convert.py
Last active Mar 26, 2022
Convert date format in file names and contents from Roam Research default style to Obsidian default style.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import os | |
from dateutil.parser import parse | |
path = '' #insert file path to your vault here | |
### Convert date format in file content | |
for root, dirs, files in os.walk(path): | |
files = [f for f in files if re.match(r'.*\.md', f)] # only keep files end with `.md` | |
#TODO: could better ignore all dirs with `.` like `.git` | |
for f in files: | |
fullpath = (os.path.join(root, f)) | |
with open(fullpath, 'r') as f: #opens each .md file | |
contents = f.read() #reads the contents | |
#substitutes dates with the format [[April 20th, 2020]] for [[2020-04-20]] | |
new_contents = re.sub(r'(?<=\[)[\w]+\s\d{1,2}\w{1,2},\s\d{4}(?=\])', | |
lambda x: str(parse(x.group(0), ignoretz=True)).split(" ")[0], contents, flags=re.M) | |
with open(fullpath, 'w') as f: | |
f.write(new_contents) #writes the files with the new substitutions | |
### Convert daily notes names | |
for root, dirs, files in os.walk(path): | |
files = [f for f in files if re.match(r'[\w]+\s\d{1,2}\w{1,2},\s\d{4}\.md', f)] | |
for f in files: | |
fullpath = (os.path.join(root, f)) | |
new_fullpath = re.sub(r'[\w]+\s\d{1,2}\w{1,2},\s\d{4}', | |
lambda x: str(parse(x.group(0), ignoretz=True)).split(" ")[0], fullpath, flags=re.M) | |
os.rename(fullpath, new_fullpath) | |
Hi Joilence! Thanks for making this script!! I'd like to convert to ISO format without dashes ('20210220' instead of '2021-02-20'), can you help me with that? Best, Koen
You have to adapt these parts for that:
re.sub(r'(?<=\[)[\w]+\s\d{1,2}\w{1,2},\s\d{4}(?=\])',
lambda x: str(parse(x.group(0), ignoretz=True)).split(" ")[0], contents, flags=re.M)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Joilence! Thanks for making this script!!
I'd like to convert to ISO format without dashes ('20210220' instead of '2021-02-20'), can you help me with that?
Best, Koen