Change file (pictures, videos) timestamp from filename


Question

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.


Answer

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 } .


Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes