{"id":1884,"date":"2023-09-08T16:37:38","date_gmt":"2023-09-08T14:37:38","guid":{"rendered":"https:\/\/blogs.gentoo.org\/mgorny\/?p=1884"},"modified":"2023-09-08T16:37:38","modified_gmt":"2023-09-08T14:37:38","slug":"a-script-to-set-file-timestamps-based-on-filenames","status":"publish","type":"post","link":"https:\/\/blogs.gentoo.org\/mgorny\/2023\/09\/08\/a-script-to-set-file-timestamps-based-on-filenames\/","title":{"rendered":"A script to set file timestamps based on filenames"},"content":{"rendered":"<p>I&#8217;ve noticed that SyncThing didn&#8217;t seem to preserve modification timestamps for the photos and videos transferred from my phone.  Since this messed up sorting by date, and the timestamps are present in filenames, I&#8217;ve made a trivial script to set mtimes appropriately.<br \/>\n<!--more--><br \/>\nNote that it assumes that the filenames have local time for Poland.<\/p>\n<pre>#!\/usr\/bin\/env python\r\n\r\nimport datetime\r\nimport os\r\nimport re\r\nimport zoneinfo\r\n\r\n\r\n# e.g. IMG_yyyymmdd_hhmmss...\r\nNAME_REGEX = re.compile(r\"\\w+_(\\d\\d\\d\\d)(\\d\\d)(\\d\\d)_(\\d\\d)(\\d\\d)(\\d\\d)\")\r\n\r\nfor f in os.listdir(\".\"):\r\n    if m := NAME_REGEX.match(f):\r\n        dt = datetime.datetime(*[int(g, 10) for g in m.groups()],\r\n                               tzinfo=zoneinfo.ZoneInfo(\"Poland\"))\r\n        os.utime(f, (dt.timestamp(), dt.timestamp()))<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve noticed that SyncThing didn&#8217;t seem to preserve modification timestamps for the photos and videos transferred from my phone. Since this messed up sorting by date, and the timestamps are present in filenames, I&#8217;ve made a trivial script to set mtimes appropriately.<\/p>\n","protected":false},"author":137,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[1],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/posts\/1884"}],"collection":[{"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/users\/137"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/comments?post=1884"}],"version-history":[{"count":2,"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/posts\/1884\/revisions"}],"predecessor-version":[{"id":1886,"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/posts\/1884\/revisions\/1886"}],"wp:attachment":[{"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/media?parent=1884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/categories?post=1884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.gentoo.org\/mgorny\/wp-json\/wp\/v2\/tags?post=1884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}