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.