Skip to content

Instantly share code, notes, and snippets.

@DarkArc
Created April 16, 2020 04:38
Show Gist options
  • Save DarkArc/f265199aada00ff7cc869a1b4a7f19e2 to your computer and use it in GitHub Desktop.
Save DarkArc/f265199aada00ff7cc869a1b4a7f19e2 to your computer and use it in GitHub Desktop.
A naive script to walk the current directory looking for Standard Notes txt files and renaming them to be either md or html files with cleaned up names
# Copyright (c) 2020 Wyatt Childers.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import re
def detect_extension(file_path):
with open(file_path) as f:
while True:
c = f.read(1)
if c.isspace():
continue
if c == '<':
return '.html'
else:
return '.md'
file_match_regex = re.compile("(.*)(-[a-z0-9]+.txt)$")
for dirpath, dirnames, filenames in os.walk('.'):
for filename in filenames:
match = file_match_regex.search(filename)
if not match:
continue
file_path = os.path.join(dirpath, filename)
true_file_ext = detect_extension(file_path)
base_file_name = match.group(1)
new_file_path = os.path.join(dirpath, base_file_name + true_file_ext)
os.rename(file_path, new_file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment