How can I change my pictures' and videos' date on my phone by their filenames? Yesterday, I moved a bunch of old pictures and videos to my phone, using a FTP server I installed on my phone. Everything went fine, but today I discovered that the date of all the copied photos and videos was set to yesterday. As a result, my photos and videos are now showing up in the wrong order in the Gallery app.

(Click image to enlarge)



Luckily, the date and time is still preserved in the filenames. Right now, I want to restore the photos' and videos' date using their filenames.

I'm interested in a way to do so. I don't really care whether it's by using an app or script or something else.

I'm aware of the fact that I can copy the files from my computer to the phone again using a different protocol, but that is not what I'm looking for.
I ended up writing a rather dirty script to do this. For this to work, I'm assuming you have a rooted device, and the directory that contains the pictures and videos is /sdcard/DCIM/Camera. If otherwise, change the script.


Download BusyBox and install it. This is needed for executing Linux shell commands.
Create a script called script.sh, containing this block of code, and put it on the root of your sd card.

cd DCIM/Camera
echo "Navigated to `pwd`"

ls

for f in *.jpg *.3gp *.m4v
do

  [ -e $f ] || continue    ## testing if the file exists
  NEWDATE=`echo $f | sed 's|IMG_||g' | sed 's|VID_||g' | sed 's|_||g' | \
  sed 's|.3gp||g'  | sed 's|.m4v||g' | sed 's|.jpg||g' | sed 's|........|&amp;\.|g'`

  echo "Touching date $NEWDATE into $f..."

  touch -t "$NEWDATE" "$f" ## using touch to insert the new date into the file

done

Start adb shell, and execute:

su
mount -o remount rw /sdcard


to make sure you can actually edit the files
Move to your sd card:

cd /sdcard

Execute the shell script:

sh script.sh

Now start the Gallery app, and wait. Especially if you have a large file collection, it may take Android a while to sort everything and update the cache.