Set modification date and time from filename

From WickyWiki
Revision as of 18:13, 24 February 2015 by Wilbert (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Set the file 'modified date' from the filename of the form "yyyyMMdd-hhmm*.jpg" or "yyyyMMdd-*.jpg", this script returns the statements.

Using the tool jhead the jpg Exif date/time can be set from the filename.

#!/bin/bash
for file in *.jpg
do
	year=${file:0:4}
	month=${file:4:2}
	day=${file:6:2}
	hour=${file:9:2}
	minute=${file:11:2}
	if ! [ "${hour}${minute}" -eq "${hour}${minute}" ] 2> /dev/null
	then
		hour="00"
		minute="00"
	fi
	if ! [ "${year}${month}${day}" -eq "${year}${month}${day}" ] 2> /dev/null
	then
		echo "#'$file' not containing a date"
	else
		echo "touch -t \"${year}${month}${day}${hour}${minute}\" \"${file}\""
		echo "jhead -ts$year:$month:$day-$hour:$minute:00 \"$file\""
	fi
done