フォルダの中にある画像ファイル名を使って変更日を設定する

以下のような連番のファイルがあるとする。

170-_001.jpg
170-_002.jpg
170-_003.jpg

ファイル名から数字を抽出して、

170-_001.jpg → 170001

この数字を「」をベースにして、日時に変換し、ファイルの変更日を修正する。

↓ こんな感じ。

170-_001.jpg → 170001 → 2000/03/28 01:21:00
170-_002.jpg → 170002 → 2000/03/28 01:22:00
170-_003.jpg → 170003 → 2000/03/28 01:23:00

これで、Google Photos にアップロードすれば、名前順(日付順)で並んでくれる。


#! /bin/sh

echo "start!"

root_path="$1"

cd "$root_path"

FILE_EXSIT_FLG=0

for pathfile in find -P ./ -type f -maxdepth 1 -iname "*.jpg" -o -iname "*.png"; do
    FILE_EXSIT_FLG=1
done

find "$root_path" -type d | sort | while read FILE01
do

if [ "${FILE_EXSIT_FLG}" = 0 ]; then
    if [ "$root_path" = "${FILE01}" ]; then
        continue
    fi
fi

cd "${FILE01}"

for file in *
do #170-_001.jpg
    n=${file%.*} #170-_001
    n=${n/_/} #170-001
    n=${n/-/} #170001
    #n=expr $n * 1

    y=0
    m=0
    d=0
    h=0

    if [ expr $n / 518400 -gt 0 ] ; then # 518400 min = 1 year(365 day)
        y=expr $n / 518400
        n=expr $n % 518400
    else
        n=$n
    fi

    if [ expr $n / 43200 -gt 0 ] ; then # 43200 min = 1 month(30 day)
        m=expr $n / 43200 + 1
        n=expr $n % 43200
    else
        m=1
        n=$n
    fi

    if [ expr $n / 1440 -gt 0 ] ; then # 1440 min = 1 day
        d=expr $n / 1440 + 1
        n=expr $n % 1440
    else
        d=1
        n=$n
    fi

    if [ expr $n / 60 -gt 0 ] ; then # 60 min = 1 hour
        h=expr $n / 60
        n=expr $n % 60
    else
        n=$n
    fi

    echo "y = $y"
    echo "m = $m"
    echo "d = $d"
    echo "h = $h"
    echo "n = $n"

    y0000=expr $y + 2000
    m0="0$m"
    d0="0$d"
    h0="0$h"
    n0="0$n"
    m00=echo $m0 | rev | cut -c 1-2 | rev
    d00=echo $d0 | rev | cut -c 1-2 | rev
    h00=echo $h0 | rev | cut -c 1-2 | rev
    n00=echo $n0 | rev | cut -c 1-2 | rev

    /usr/local/bin/exiftool -P -overwrite_original -alldates="${y0000}:${m00}:${d00} ${h00}:${n00}:00" "$file"

    touch -t "${y0000}${m00}${d00}${h00}${n00}" "$file"

done

done

echo "finish!"

数字だけ使ってたらバグったので、日付を使用した以下の記事を参照。