Script to Organize Dropbox’s Camera Upload folder

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.

DropboxCameraUpload

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:

😉

Comments

  1. 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 :)”)

  2. 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.

  3. 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

    1. 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.

      ################################################################################################
      #
      #	Script to organize pictures uploaded from Mobile Devices to Dropbox's Camera Upload folder
      #
      #	Daniel Spillere Andrade - www.danielandrade.net
      #
      ################################################################################################
      
      import os, time
      import glob
       
      #Define Pictures Folder
      #You may have to change this!
      folder = "Z:\\Dropbox\\Camera Uploads\\"
       
      fileFormats = ['JPG','jpg', 'MOV', 'mov', 'PNG', 'png', 'mp4', 'MP4'];
      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)
      
      for files in picPath:
      	picName = files.split('\\')
      	filename = picName[-1][:-4].split(' ')
      	date = filename[0].split('-')
      	hour = filename[1]
      	print 
      
      	dateYear = date[0]
      	dateMonth = date[1]
      	dateDay = date[2]
      	month = months[int(dateMonth)-1]
      	monthNum = str(int(dateMonth)).zfill(2) 
      
      	#folder exists? if not, create them!
      	if not os.path.exists(folder + dateYear):
      	    print 'Making dir:' + folder + dateYear
      	    os.makedirs(folder + dateYear)
      	if not os.path.exists(folder + dateYear + '\\' + monthNum + '-' + month):
      	    print 'Making dir:' + folder + dateYear + '\\' + monthNum + '-' + month
      	    os.makedirs(folder + dateYear + '/' + monthNum + '-'  +month)
      
       
      	#Move files
      	print 'Movendo: ' + files + ' --&gt; ' +  folder + dateYear + '\\' + monthNum + '-' + month + '\\'
      	os.rename(picName[-1],folder + dateYear + '\\' + monthNum + '-' + month + '\\' + picName[-1])
      
      	
      print "Done :)"
      

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.