0

I recently freed myself from my MacOS system. On of the big concerns I had was preserving the fourteen years worth of family images I saved up in iPhoto.

I decided to export all 17000+ images, and put them into a folder tree organized by year, month, and day. The problem is that the files themselves contain no information about when the images were taken. The only property that identifies when the images were taken is the folder in which they sit.

Right now, my 'Pictures' folder tree looks like this:

 2004/    
 2005/    
 2006/    
 2007/    
 2008/    
 2009/    
 2010/    
 2011/    
 2012/    
      01_January/    
      02_February/    
      03_March/    
      04_April/    
      05_May/    
      06_June/       
           18June, 2012/    
                File - 001.jpg     
                File - 002.jpg    
                File - 003.jpg    
                etc...    
           24June, 2012/    
                File - 001.jpg    
                File - 002.jpg    
                File - 003.jpg    
                etc...    
      07_July/    
      08_August/    
      09_September/    
      10_October/    
      11_November/    
      12_December/
 2013/    
 2014/    
 2015/    
 2016/    
 2017/    
 2018/   

Each year directory contains twelve month directories. Each month directory contains a day directory only if there are images for that day.

What I want to do is append all the image filenames so that they include elements from their superior directories:

Example:

 ./2012/06_June/18June, 2012/      
      File - 001.jpg   ------->   2012-06-18_File - 001.jpg    
      File - 002.jpg   ------->   2012-06-18_File - 002.jpg    
      File - 003.jpg   ------->   2012-06-18_File - 003.jpg     
      etc...

Can someone help me find a way to do this? Python or Bash script, I'm okay learning how to do this in either. Thanks.

muru
  • 4,723
  • 1
  • 34
  • 78

3 Answers3

0

Assuming you are in your Pictures folder, you may try the following code. Please check first that the new names are correct and then uncomment the commented line.

from glob import glob
import os
import re
for i in os.scandir(os.getcwd()):
    if i.is_dir():
        year = i.name
        os.chdir(i.name)
        for j in os.scandir(os.getcwd()):
           if j.is_dir() and os.listdir(j.name):
               month = re.compile(r'(\d+)').split(j.name)[-2]
               os.chdir(j.name)
               for k in os.scandir(os.getcwd()):
                   if k.is_dir():
                       day = re.compile(r'(\d+)').split(k.name)[-4]
                       os.chdir(k.name)
                       for l in glob('*.jpg'):
                           newName = year + '-' + month + '-' + day + '_' + l
                           print(l)
                           print(newName)
                           # os.rename(l, newName)
                       os.chdir('..')
               os.chdir('..')
        os.chdir('..')
Patol75
  • 4,342
  • 1
  • 17
  • 28
  • 1
    You don't need to use external tools like `mv` to rename files with Python. See [How to rename a file using Python](https://stackoverflow.com/q/2491222/4154375). – pjh Nov 19 '18 at 21:10
0

This is a (Shellcheck-clean) Bash solution:

#! /bin/bash

shopt -s nullglob   # Globs that match nothing expand to nothing

for file_path in [12][0-9][0-9][0-9]/[01][0-9]_*/[0-3][0-9]*/*.jpg ; do
    IFS=/ read -r year month_name day_month_year file_name <<<"$file_path"
    month=${month_name%%[^0-9]*}
    day=${day_month_year%%[^0-9]*}

    day_month_year_path=${file_path%/*}
    new_file_name=${year}-${month}-${day}_${file_name}
    new_file_path=${day_month_year_path}/${new_file_name}

    printf '    %s   ------->   %s\n' \
        "$file_path" "$new_file_path" >&2

    #UNCOMMENT mv -- "$file_path" "$new_file_path"
done

Run it in the directory containing the year directories (2004, ...).

If you are happy that it will rename files correctly, remove the #UNCOMMENT from the start of the mv line and run it again to actually rename the files.

See How do I split a string on a delimiter in Bash? for details of how the IFS=/ read -r ... <<<"$file_path" line works.

See Removing part of a string (BashFAQ/100 (How do I do string manipulation in bash?)) for information about ${var##pattern} and ${var%%pattern}.

See glob - Greg's Wiki for information about patterns such as the one on the for line, */, and [^0-9]*.

pjh
  • 6,388
  • 2
  • 16
  • 17
-1

The solution i have is using python, Just looking for all file in your PATH and using sum of String to create your image name