Skip to content

Instantly share code, notes, and snippets.

@roustem
Created October 14, 2021 22:50
Show Gist options
  • Save roustem/13ef1355eb5f0e85279ae25ed7126bb8 to your computer and use it in GitHub Desktop.
Save roustem/13ef1355eb5f0e85279ae25ed7126bb8 to your computer and use it in GitHub Desktop.
import os
import re
date_re = re.compile("(\d\d\d\d)(\d\d)(\d\d).md")
for root, dirs, files in os.walk("./"):
for filename in files:
if filename.endswith(".md"):
new_filename = filename
m = date_re.match(filename)
if m:
new_filename = m.group(1) + "-" + \
m.group(2) + "-" + m.group(3) + ".md"
file = open(os.path.join(root, filename), "r")
contents = file.read()
file.close()
new_contents = re.sub(
"(202\d)(\d\d)(\d\d)", "\g<1>-\g<2>-\g<3>", contents)
if new_contents != contents or new_filename != filename:
# print(filename + " => " + new_filename)
# print(new_contents)
file = open(os.path.join(root, new_filename), "w")
file.write(new_contents)
file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment