Skip to content

Instantly share code, notes, and snippets.

@DarrenCattle
Last active May 28, 2017 02:52
Show Gist options
  • Save DarrenCattle/0d58cbf19999aea8ba856c545a7e6691 to your computer and use it in GitHub Desktop.
Save DarrenCattle/0d58cbf19999aea8ba856c545a7e6691 to your computer and use it in GitHub Desktop.
script that will rename your files based on when they were created
# File Organizer
# Darren Cattle
# May 2017
# Python3
import os
import datetime
import calendar
def conv_unix(timestamp):
return datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %Hh%Mm%Ss')
def create_dir(directory):
if not os.path.exists(directory):
os.makedirs(directory)
drive = "F:/"
src = "feed/"
container = drive + "drives/"
path = drive + src
create_dir(path)
files = []
times = []
for file in os.listdir(path):
files.append(path + file)
times.append(os.path.getmtime(path + file))
datetimes = list(map(conv_unix, times))
years = list(set(map(lambda s : s[0:4], datetimes)))
months = list(set(map(lambda s : s[5:7], datetimes)))
# + '_' + calendar.month_name[int(s[5:7])]
days = list(set(map(lambda s : s[8:10], datetimes)))
print(years, months, days)
for year in years:
cd = container + year
create_dir(cd)
for month in months:
cd += '/' + month
create_dir(cd)
for i in range(0,len(files)):
file = files[i]
dt = datetimes[i]
result = container
result += dt[0:4] + '/' + dt[5:7] + '/'
result += dt[8:10] + '/'
create_dir(result)
result += dt[11:20] + '.avi'
print(file + ' to ' + result)
os.rename(file, result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment