Dropbox has a great feature called Camera Upload, where you can automatically upload pictures from your phone, making life for lazy people like me easier. The problem is that I take so many pictures with my phone that the folder ends up with a huge amount of files, making the task of browsing and viewing them slow and annoying.
To fix this problem, I created a python script that will automatically organize pictures inside folders Year > Month.
How to use:
In order to use the script, put the file organize.py inside the ‘Camera Upload’ folder and run it from the terminal:
python organize.py
Enjoy!
Code:
😉
Thanks heaps…
I have updated to allows for different file name and more formats. i.e. _yyyymmdd_hour., _yyyy-mm-dd_hour., yyyy-mm-dd hour. etc…
It also uses the modified date if it is unable to derive it from the name. Changed the folder structure to work with windows:
################################################################################################
#
# Script to organize pictures uploaded from Mobile Devices to Dropbox’s Camera Upload folder
#
# Daniel Spillere Andrade – http://www.danielandrade.net
#
################################################################################################
# Supported files _yyyymmdd_hour., _yyyy-mm-dd_hour., yyyy-mm-dd hour.
import os, time
import glob
import datetime
#Return lengths in string
def chunkstring(string, length):
return (string[0+i:length+i] for i in range(0, len(string), length))
#Define Pictures Folder
#You will have to change this!
folder = ”
fileFormats = [‘JPG’,’jpg’, ‘jpeg’, ‘JPEG’, ‘MOV’, ‘mov’, ‘PNG’, ‘png’, ‘mp4’, ‘MP4’, ‘3gp’, ‘3GP’, ‘gif’, ‘GIF’]
months = [‘January’,’February’, ‘March’, ‘April’, ‘May’, ‘June’, ‘July’, ‘August’, ‘September’, ‘October’, ‘November’, ‘December’]
picPath = []
#Generate list of files
for formats in fileFormats:
picPath = picPath + glob.glob(folder + “*.”+formats)
picPath = list(set(picPath))
for files in picPath:
print (“”)
try:
picName = files.split(‘\\’)
if “_” in picName[-1][:-4]:
filename = picName[-1][:-4].split(‘_’)
else:
filename = picName[-1][:-4].split(‘ ‘)
if len(filename)>=3:
filestart = 1
else:
filestart = 0
try:
if “-” in filename[filestart]:
date = filename[filestart].split(‘-‘)
dateYear = date[0]
dateMonth = date[1]
dateDay = date[2]
else:
date = list(chunkstring(filename[filestart], 2))
dateYear = date[0]+date[1]
dateMonth = date[2]
dateDay = date[3]
if not (len(dateYear) is 4 and len(dateMonth) is 2 and 1<=int(dateMonth) ‘ + folder + dateYear + ‘\\’ + monthNum + ‘-‘ + month + ‘\\’)
os.rename(picName[-1],folder + dateYear + ‘\\’ + monthNum + ‘-‘ + month + ‘\\’ + picName[-1])
except:
print (“Couldn’t sort: “+files)
continue
print (“\nDone :)”)
Remember to change the Dropbox folder location.
Great, thanks for sharing!
Nice one!
If you change the following line from:
“`os.rename(picName[-1],folder + dateYear + ‘/’ + monthNum + ‘-‘ + month + ‘/’ + picName[-1])“`
to
“`
os.rename(files,folder + dateYear + ‘/’ + monthNum + ‘-‘ + month + ‘/’ + picName[-1])
“`
then you don’t need to run the script in the same directory as the photos. Caught me out for a sec.
Thanks for sharing.
Thank you! Happy it helped.
I have been using this script for years now 😉
Very helpful. Many thanks! 😀
Thanks! 😉
Don’t forget to check the improved version in Golang .
Cheers
Really ? Did this work for you on Windows ? Did you post it anywhere ? Tried copying fro here but nothing worked as it should. The original code looks good and commented. This last one looks like its got lots of errors or the copy and paste did not work as expected.
Lines like :
if not (len(dateYear) is 4 and len(dateMonth) is 2 and 1<=int(dateMonth) ‘ + folder + dateYear + ‘\\’ + monthNum + ‘-‘ + month + ‘\\’)
???
If you have the windows code I'd be interested.
Cheers
Here is the windows script without all the tweaking that I found not necessary. Almost the original.
Thanks for this Daniel. I found it easier to tweak the original than to go with the other one you have as I am already familiar with python. At work we have a bunch of scripts that also recognize the system as Win/ Linux and Macs and run depending on the system.
Could be an option if you are bored one day.
Cheers.
Awesome, thanks for the file!
I am sure there is a way to improve this or make system agnostic!
Cheers