Change file (pictures, videos) timestamp from filename
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} .