Changing Filename date format to yyyy-mm-dd Issues

Parameters for the Advanced Renamer
Mode: Regular Expressions
Old name: ^(\d\d\d\d)(\d\d)(\d\d)(.*)
New name: \1-\2-\3\4
Ignore extension: checked

1 Like

Fantastic!

Although I do not yet fully understand the code, I am sure that with you it will work brilliantly.

I will make sure to try and gradually work my way up to applying this to 10, then 100, then 1000, then 10,000 files and so on :face_with_peeking_eye:

Somehow I can't seem to google the characters, so I'm going to guess...

^ = beginning of a string?
(\d\d\d\d) = four digits in a row, bracketed to reuse them at a certain place in the new name?
(\d\d) = ditto for two digits?
(.*) = this I don't seem to get. It looks like the file extension, but it probably is not. Here the portion
[any character string] should be matched, shouldn't it?

And this will only pick up on filenames that actually start with at least eight digits directly after each other, right?

Actually, I didn't ask the original question correctly, because in fact the naming pattern of the existing files and folders looks like this (added a blank ⎵ after the date, and included folders)
YYYYMMDD⎵[any character string] (folders) or
YYYYMMDD⎵[any character string][.any file extension] (files)

How would I have to modify the code to make it respond (exclusively) to this pattern?

Or would it be better to make one RegEx for files and a second one for folders?

To understand things like (.*), see RegExp basics: Removing characters from start/end of names (pinned at the top of the forum's Rename area if you need to find it again) or Regular Expression Syntax in the manual (direct link to the page in the Opus Manual menu at the top-right of the forum for quick access).

1 Like

Thanks Leo!

From your link:

...I believe I just learned that the (.*) part simply matches my [any character string] no matter what, and the brackets are again used:

Just add the space. Will work for both files and folders (thanks to the Ignore extension option).

1 Like

I see, thanks very much again!

I have not read the comments in this discussion in detail, so I apologize if my suggestion is not suitable.
If the date at the start of the filename exists in the metadata (File Created date or File Modified date - or other depending on the file type), how about:
1: Removing the date portion of the filename.
2: Adding the date in the format you want to the beginning of the filename.

I have used Photo Mechanic to do this type of thing many times. I assume it can be done w Directory Opus, though I don't know how one would do it.

That could be an easier way to do the original question from 4 years ago. The thread has drifted to something slightly different now, though.

(The new/recent question would've been better in its own thread, really.)

Hello. Revisiting my original thread.

This stopped working for the following:

  • 20220903 outputs as 2090-02-023, instead of 2022-09-03

However, the original 20180102, outputs correctly as 2018-01-02.

So they're still using random date formats for each new folder, years after the initial mess was cleaned up? Could the problem be fixed at the source? Or are the folder names outside anyone's control, and not all being made by the same people or organisation?

You'll never get a script or regex that can handle every possible date format because they're going to clash with each other and have ambiguities (unless they use month names and separators between components, but they aren't using those it seems).

If the folders have the right dates in their Modified or Created timestamps, you can use those to set the folder names without having to try to parse the existing names.

I would think it should work though

If the digit is 8 number characters long and starts with a 20xx, it must be the year portion, as month can only go 01 to 12

I fixed it.


function lpad( val, pad ) {
  if (!pad) pad='';
  var rtn=val.toString();
  return (rtn.length<pad.length) ? (pad+rtn).slice(-pad.length) : val;
}

function ProduceNewName(oldname, matched, day, month, year) {
  var newDate  = lpad(year, "0020") + "-" + lpad(month, "00") + "-" + lpad(day, "00");
  DOpus.OutputString("'" + oldname +"' - '" + matched +"' - '" + newDate +"' day:'" + day +"' month:'" + month + "' year:'"+year+"'");
  return oldname.replace(matched, newDate);
}

function OnGetNewName(getNewNameData)
{
	var item = getNewNameData.item;
	//DD-MM-YYYY D-M-YY
	var reg = new RegExp('([0-1]?\\d)[-.]([0-3]?\\d)[-.]((?:19|20)?[0-9]{1,2})|((?:19|20)?[0-9]{1,2})[-.]([0-1]?\\d)[-.]([0-3]?\\d)');
	var regexMatch = reg.exec(item.name);
  	if (regexMatch != null) {
		DOpus.OutputString(item.name + " matched 1 DD-MM-YYYY or YYYY-MM-DD");
		getNewNameData = ProduceNewName(item.name, regexMatch[0], 
			regexMatch[2] + regexMatch[6],
			regexMatch[1] + regexMatch[5],
			regexMatch[3] + regexMatch[4]);
		return getNewNameData;
    }

		//YYYYMMDD 
	var reg = new RegExp('((?:19|20)?[0-9]{2})([0-3]?\\d)([0-3]?\\d)');
	var regexMatch = reg.exec(item.name);
    if (regexMatch != null) {
		DOpus.OutputString(item.name + " matched 3 YYYYMMDD");
	    getNewNameData = ProduceNewName(item.name, regexMatch[0], regexMatch[3], regexMatch[2], regexMatch[1])
		return getNewNameData;
    }

	//DDMMYYYY
	var reg = new RegExp('([0-1]?\\d)([0-3]?\\d)((?:19|20)[0-9]{2})');
	var regexMatch = reg.exec(item.name);
    if (regexMatch != null) {
		DOpus.OutputString(item.name + " matched 2 DDMMYYYY");
	    getNewNameData = ProduceNewName(item.name, regexMatch[0], regexMatch[2], regexMatch[1], regexMatch[3])
		return getNewNameData;
    }

	//Failed, true means no change.
	return true;
}

image