File name to modified date/time?

With thanks to MrC, who has really done a great job in creating a script which may do most (if not all) what you need, you may wish to have a look at
thread: Change modified date after filename

specifically his last script, posted on 23 Aug 2013.

I admit, it is a bit of a tailor-made script and you need to test it first.

For example : your filename reads:
Filename 2011-07-29_082609.mp3

if you were to rename the filenames by removing the underscore (Filename 2011-07-29 082609.mp3), it will run just fine.

I have created a "Date from filename"-button to do the redates.

Good luck!


Dear Leo et al,

I have just tried your 2017 / Dopus 12 script and works fine with the filename format "YYYY-MM-DD_HH-MM-SS". I have even made a minor edit for changing the Creation Date attribute, instead of the Modified Date.

How this script can be modified for extracting the date from a format of "YYYYMMDD_" from either the filename OR from the folder-name? (two different scripts, on two buttons, are fine!)

Thank you!

Change these two lines to be like this:

...
	re.Pattern = "^(\d\d\d\d\d\d\d\d)_.*$"
...
			strDateTime = re.Replace(selItem.name_stem_m, "$1")
...

This looks at the parent folder name instead of the file name:

Option Explicit
Function OnClick(ByRef clickData)
	Dim strDateTime, strCommand, selItem, re, cmd

	Set re = new RegExp
	re.IgnoreCase = True
	re.Global = False
	' Folder name should be something like "20110729_anything"
	re.Pattern = "^(\d\d\d\d\d\d\d\d)_.*$"

	Set cmd = clickData.func.command
	cmd.ClearFiles

	For Each selItem in clickData.func.sourcetab.selected
		If (re.Test(selItem.Path.filepart)) Then
			' Set strDateTime to a string like "20110729"
			strDateTime = re.Replace(selItem.Path.filepart, "$1")
			' DOpus.Output "DT  = " & strDateTime
			strCommand = "SetAttr FILE=""" & selItem.RealPath & """ MODIFIED=""" & strDateTime & """"
			' DOpus.Output "CMD = " & strCommand
			cmd.RunCommand strCommand
		End If
	Next
End Function

(Change MODIFIED to CREATED as well if you want to set that timestamp instead.)

As expected, works smoothly! A last point, how the scripts may also set the TIME as 00:00:00?

Thank you.

Change "$1" to "$1 00:00:00"

Thank you very much dear Leo!

1 Like

I just ran into the same issue. Since this topic is like 10 years old, do we have now some new moments? as Opus command set has significantly raised in power since...

I need to set the modified date from the first part of the filenames like this:

2021-03-10_221651

My locale date format is "dd.MM.yyyy" - I'm not sure how it would interfere with all that business?

Thanks in advance.

The topic is old but the solution in the 4th post was "Updated for 2017 / Directory Opus 12" and is still current.

You'd need to adjust the regex search and replace strings. Probably something like these:

	re.Pattern = "^(\d\d\d\d)\.(\d\d)\.(\d\d) - .*"

...

			strDateTime = re.Replace(selItem.name_stem_m, "$1-$2-$3")

Try it on some test files first, of course.

1 Like

Is it time to split the thread? Into one about renaming and a second one about attribute setting?

This could be a good idea. I kinda got lost over there. :slight_smile:

Apart from 3 short posts in the middle, the whole thread is about setting timestamps from filenames, including the start of the thread and the new posts this week.

(There was a Rename command in the original, old solution, because that was a way to run scripts in the past, before there was more widespread scripting support. It wasn't really renaming anything though, and the newer solution doesn't need that method.)

Nevertheless, the script has worked flawlessly in my case. Thanks @Leo, appreciate it. :slight_smile:

1 Like

I see that the SetAttr internal commands are used in this script https://www.gpsoft.com.au/help/opus12/index.html#!Documents/SetAttr.htm

I was able to modify the script to write to the Standard Properties 'Date Created' field using 'CREATED' in place of 'MODIFIED'.

Video dates are a big problem in my line of work, and often the correct date is only in a filename or folder name. Is there a command argument to write the date from the filename to the Movie Properties 'Release Date' field? I have used Graphic Converter (for Mac only) for this task, but am looking for a Windows/PC solution.

:warning: This is not tested, so try it on some backup video files first in case I got something wrong.

This type of command should set the release date field on file types Opus knows how to do so:

SetAttr FILE="<filepath>" META="releasedate:<YYYYMMDD>"

Below is the altered version of the old script.

As well as changing the command it runs, I made it use selected_files instead of selected, so it doesn't try to do this to folders. Folders don't have a release date property, so it wouldn't make sense to try to handle them.

Option Explicit
Function OnClick(ByRef clickData)
	Dim strDateTime, strCommand, selItem, re, cmd

	Set re = new RegExp
	re.IgnoreCase = True
	re.Global = False
	' File name should be something like "20110729_anything"
	re.Pattern = "^(\d\d\d\d\d\d\d\d)_.*$"

	Set cmd = clickData.func.command
	cmd.ClearFiles

	For Each selItem in clickData.func.sourcetab.selected_files
		If (re.Test(selItem.Path.filepart)) Then
			' Set strDateTime to a string like "20110729"
			strDateTime = re.Replace(selItem.Path.filepart, "$1")
			' DOpus.Output "DT  = " & strDateTime
			strCommand = "SetAttr FILE=""" & selItem.RealPath & """ META=""releasedate:" & strDateTime & """"
			' DOpus.Output "CMD = " & strCommand
			cmd.RunCommand strCommand
		End If
	Next
End Function

Thank you. Wonderful. It worked on MP4 files.

Please allow me to revert on this subject: one way or the other the script does not work. That is to say, nothing happens at -my- side.

I tried both the .dcf (at the top) and the script here at the bottom.

Filename is: (e.g.)
20221023_173910.jpg
20200423_134740.mp4

and I want to have the modified date to read as per the file name.

Any suggestions?

Thanks!

While I can't answer your question, I am very curious as to why you would want to rename any photos or videos by their Modified date? As a professional photo organizer, I would never do that because when the files are sorted by their Date Taken or Capture Date or Media Created Date (column name will depend on the application) the files will not sort by the date in the filename - unless that date happens to match the Date Taken or Capture Date or Media Created Date.

The first script uses a different name/date format, and I think the other script looks at the parent folder name rather than the file name.

This one should work (detail was above, but involved taking the first script and modifying two lines in it, which I've done here so it's all in one place):

  • Filename to Modified Time.dcf (1.9 KB)
  • Expected name/date format: YYYYMMDD_*
  • Works on filename (not parent folder name).
  • Changes Modified timestamp.
  • Changes the date part, leaving the time part as whatever it currently is.
  • Affects all selected items (both files and folders).
Option Explicit
Function OnClick(ByRef clickData)
	Dim strDateTime, strCommand, selItem, re, cmd

	Set re = new RegExp
	re.IgnoreCase = True
	re.Global = False
	' Name, ignoring extension, should be something like "20110729_Xyz"
	re.Pattern = "^(\d\d\d\d\d\d\d\d)_.*$"

	Set cmd = clickData.func.command
	cmd.ClearFiles

	For Each selItem in clickData.func.sourcetab.selected
		If (re.Test(selItem.name_stem_m)) Then
			' Set strDateTime to a string like "20110729"
			strDateTime = re.Replace(selItem.name_stem_m, "$1")
			' DOpus.Output "DT  = " & strDateTime
			strCommand = "SetAttr FILE=""" & selItem.RealPath & """ MODIFIED=""" & strDateTime & """"
			' DOpus.Output "CMD = " & strCommand
			cmd.RunCommand strCommand
		End If
	Next
End Function

Super.
Many thanks Leo! Matter solved.
(FWIW only, for my personal use I prefer the time be changed as well, hence I slightly updated the regex part to: "^(\d{8})(\s|_|-)(\d{6})$" and the replace to "$1 $3")
Anyway, thanks again.

Thanks. Yes I know. OTOH sometimes files do not have EXIF details (e.g. screenshots).
MP4 files no not have EXIF details either.
I believe the created date-time attributes are stored under QuickTime and in Opus file properties "Media Created". AFAIK, when changing attributes, there is no option to select 'Media Created' date-time stamp. Then again, this would only apply to MP4 files...

In case of other video filetypes (e.g. .mkv, wmv) it is more complex (=maybe impossible?) to get the original created date time stamp.