Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

131 lines (107 sloc) 2.9 kB
import os
import sys
import re
import datetime
# Deletes the file extension of the file as well as the preciding period
# Capable of handeling video files with 3 or 4 letter file extensions
def findextension(s):
if s[len(s) - 4] == '.':
count = 4
while (count != 0):
del s[-1]
count = count - 1
elif s[len(s) - 5] == '.':
count = 5
while (count != 0):
del s[-1]
count = count - 1
# Deletes any data after the first square bracket ([)
# Usually, this indicates the end of the title
def bracketdeletion(s):
count = 0
for i in range(len(s)):
if s[i] == '[':
count = (len(s) - i)
break
while (count != 0):
del s[-1]
count = count - 1
# Searches the string for the year and deletes it and any characters after it
# Placed in order to preserve the title
def findyear(s):
count = 0
match = re.compile('\d\d\d\d').search("".join(s))
if (match):
count = len(s) - match.start()
while (count != 0):
del s[-1]
count = count - 1
# If words are seperated by periods
def deleteperiods(s):
for i in range(len(s)):
if (s[i] == '.') & (i != len(s)-1):
s[i] = ' ' #space
elif (s[i] == '.') & (i == len(s)-1):
del s[-1]
# Deletes the phrase 'DVD" and any characters after it to preserve the title
def dvdtext(s):
count = 0
match = re.compile('DVD', re.I).search("".join(s))
if (match):
count = len(s) - match.start()
while (count != 0):
del s[-1]
count = count - 1
# Removes the phrase "Part" and any characters after it to preserve the title
def removepart(s):
count = 0
match = re.compile(r'\bPart', re.I).search("".join(s))
if (match):
count = len(s) - match.start()
while (count != 0):
del s[-1]
count = count - 1
# Removes excess whitespace at the end of a filename
def removewhitespace(s):
if (len(s) > 0):
if (s[len(s)-1] == ' '):
del s[-1]
# Removes excess special characters at the end of the filename
def removespecialchars(s):
if (len(s) > 0):
match = re.compile('[^a-zA-Z0-9_ ]').search(s[-1])
if (match):
del s[-1]
# Populates the list with all the file names in a given directory structure
fileList = []
rootdir = sys.argv[1]
for root, subFolders, files in os.walk(rootdir):
for file in files:
fileList.append(list(file))
# Formats the filenames
for s in fileList:
findextension(s)
bracketdeletion(s)
findyear(s)
deleteperiods(s)
dvdtext(s)
removepart(s)
removewhitespace(s)
removespecialchars(s)
removewhitespace(s)
# Removes duplicates caused by part 1 and part 2
count = 0
while (count < len(fileList)):
if (count > 0):
if (fileList[count] == fileList[count-1]):
del fileList[count]
count = count + 1
# Sorts the list
fileList.sort()
now = datetime.datetime.now()
# Writes sorted list to file
f = open('List of Movies ' + now.strftime("%Y-%m-%d") + '.txt', 'w')
for x in fileList:
f.write("".join(x))
f.write("\n")
f.close()
Jump to Line
Something went wrong with that request. Please try again.