New version of lecturemunger
This commit is contained in:
34
old/rename.py
Normal file
34
old/rename.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import sys
|
||||
import os
|
||||
import dateutil.parser as dparser
|
||||
import re
|
||||
|
||||
target = sys.argv[1]
|
||||
|
||||
if not os.path.isdir(target):
|
||||
print('Invalid path. Try again.')
|
||||
|
||||
for entry in os.scandir(target):
|
||||
# Put file extension to one side for safekeeping
|
||||
split = entry.name.split('.')
|
||||
extension = split[1]
|
||||
name = split[0].replace(',', '').split('#')[0] # Discard all commas, and unique id doodad
|
||||
head, sep, tail = name.partition(' ') # Discard leading date
|
||||
split2 = tail.split()
|
||||
date = dparser.parse(split2[-1], fuzzy=True) # Parse the date
|
||||
folder_path = split2[0] + split2[1] # TODO Fragile
|
||||
title = re.search(r'\d+-\d+\s[\d\w\s]*', tail)
|
||||
if re.match:
|
||||
title = title.group(0).strip().replace(' ', '_')
|
||||
# Add date back
|
||||
if date is not None:
|
||||
title += "_" + date.strftime('%Y-%m-%d')
|
||||
# Rename file
|
||||
source = entry.path
|
||||
actual_folder_path = '{}/{}'.format(target, folder_path)
|
||||
dest = "{}/{}.{}".format(actual_folder_path, title, extension)
|
||||
print ("Source: " + source)
|
||||
print ("Dest: " + dest)
|
||||
if not os.path.isdir(actual_folder_path):
|
||||
os.mkdir(actual_folder_path)
|
||||
os.rename(source, dest)
|
||||
Reference in New Issue
Block a user