Change file (pictures, videos) timestamp from filename When moving files around tru different dirs and across PC / smartphone, timestamp for last modified date usually get reset to the very datetime of move / copy operation. This lead to files being non-chronologically sorted in some apps (Gallery), quite annoying especially for Images and Videos.

Usually these kind of files follows the naming convention of IMG_YYYYMMDD_HHMMSS.jpg for Images and VID_YYYYMMDD_HHMMSS.mp4 for Videos.
So I thought about combining touch and extracting character from filenames, the timestamp being in effect part of the filename itself.

I found this script at Ephestione's HQ 
for i in IMG*.jpg; do busybox touch -t ${i:4:8}${i:13:4}.${i:17:2} $i; done
but works in terminal only. I tried to make a script out of it but it doesn't work (see below)

#!/system/bin/sh
#change timestamp for images filename IMG_YYYYMMDD_HHMMSS.jpg 
for i in IMG_*.jpg
do
    touch -t ${i:4:8}${i:13:4}.${i:17:2} $i
done

#change timestamp for whatsapp images IMG-YYYYMMDD-WAnnnn.jpg
for i in IMG-*WA*.jpg
do
    touch -t ${i:4:8}1234 $i
done

#change timestamp for videos filename VID_YYYYMMDD_HHMMSS.mp4
for i in VID_*.jpg
do
    touch -t ${i:4:8}${i:13:4}.${i:17:2} $i
done

#change timestamp for whatsapp videos IMG-YYYYMMDD-WAnnnn.mp4
for i in VID-*WA*.mp4
do
    touch -t ${i:4:8}1234 $i
done


Ideally I'd like to put a script like this in the top level DCIM or Pictures directory and have it process all the Images and Videos in the subdirs. Of course I'm rooted.
Remove from the script the .${i:17:2} in for loop.

It should be:

#!/system/bin/sh
#change timestamp for images filename IMG_YYYYMMDD_HHMMSS.jpg 
for i in IMG_*.jpg
do
    touch -t ${i:4:8}${i:13:4} $i
done

#change timestamp for whatsapp images IMG-YYYYMMDD-WAnnnn.jpg
for i in IMG-*WA*.jpg
do
    touch -t ${i:4:8}1234 $i
done

#change timestamp for videos filename VID_YYYYMMDD_HHMMSS.mp4
for i in VID_*.jpg
do
    touch -t ${i:4:8}${i:13:4} $i
done

#change timestamp for whatsapp videos IMG-YYYYMMDD-WAnnnn.mp4
for i in VID-*WA*.mp4
do
    touch -t ${i:4:8}1234 $i
done


Note that the images and videos from WhatsApp according to your script have the same time 12:34.

Edit: To also go through subdirectories, change the for loops as followed:


for i in `find . -type f -name "IMG_*..jpg"`
do

j=`basename $i` 

echo $j

touch -t ${j:4:8}${j:13:4} $i


done



Explanation:

find . -type f -name ".mp5"`: find files ( -type t) whose names contains " IMG_.jpg" ( -name "IMG_*.jpg") in the current directory ( . ) and subdirectories.

j= basename $i : Get the filename (not the entire path), and assign it to j. 

touch -t ${j:4:8}${j:13:4} $i : Change  the file's modification time based on the date and time retrieved from ${j:4:8}${j:13:4} .