Reorder WhatsApp images in Google Photos according to the dates in the name

715 Views Asked by At

When I bought new Android device and installed WhatsApp there, it downloaded all media files from Drive backup. Now when I open Google Photos, all those 1000+ images are in same date, the date in which they were downloaded. WhatsApp images don't contain any EXIF data. But they contain date in their name itself (IMG-20210101-WA0001). Is there any way to sort them according to the date in their name?

1

There are 1 best solutions below

0
Sourav Kannantha B On

I noticed that Google Files app sorts files based on their modification date. So I decided to modify mtime of all files using shell script by connecting to adb:

$ cd sdcard/
$ IFS=$'\n'
$ for f in $(find Android/media/com.whatsapp/WhatsApp/Media/ -type f | grep -E -v "/WhatsApp Documents/|/.Links/|/.Statuses/|/.nomedia|/.trashed")
> do
> old_t=$(date -r "$f" +%Y%m%d%H%M.%S%N)
> dt=$(cut -d- -f2 <<< $(basename "$f"))
> new_t=$(sed -E "s/.{8}/$dt/" <<< $old_t)
> touch -m -t $new_t "$f"
> done

The above script will first loop over all files in WhatsApp folder (except WhatsApp Documents, .Links, .Statuses, .nomedia, .trashed as files there don't contain date in their names). Then get the file's mtime value, extract date from file name, replace date part in the mtime value and preserve time part. Finally using touch, it updates the mtime value of the file.

For me, it took more than half an hour to update mtime of all files once the script started executing. But apparently Google Photos doesn't use mtime to sort.

I noticed that atime was not getting updated when I access files. When I checked, storage/emulated/0 was mounted with noatime. Somewhere I had read in those cases, atime is used to store created time of file. So I decided to again modify atime of all files in the same way:

$ cd sdcard/
$ IFS=$'\n'
$ for f in $(find Android/media/com.whatsapp/WhatsApp/Media/ -type f | grep -E -v "/WhatsApp Documents/|/.Links/|/.Statuses/|/.nomedia|/.trashed")
> do
> x1=$(stat --format %X "%f")
> x2=$(stat --format %x "%f")
> old_t=$(date -d "@$x1.$(cut -d. -f2 <<< $(cut -d' ' -f2 <<< $x2))" +%Y%m%d%H%M.%S%N)
> dt=$(cut -d- -f2 <<< $(basename "$f"))
> new_t=$(sed -E "s/.{8}/$dt/" <<< $old_t)
> touch -a -t $new_t "$f"
> done

The above script works the same as previous one for the most part. Although unnecessary, I have preserved the value of old atime till nanoseconds and have just replaced the date part.

Finally photos in Google Photos are now sorted in correct order.