Changing Filename date format to yyyy-mm-dd Issues

Had the day month match reversed in one place.
I didnt spend time to verify if this caused other issues.

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[1] + regexMatch[6],
			regexMatch[2] + regexMatch[5],
			regexMatch[3] + regexMatch[4]);
		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[1], regexMatch[2], regexMatch[3])
		return getNewNameData;
    }

	//YYYYMMDD
	var reg = new RegExp('((?:19|20)?[0-9]{2})([0-3]?\\d)([0-1]?\\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;
    }

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

image

1 Like

Everything seems to work fine after a quick test.

The only thing that doesn't work, which I guess wasn't a feature requested, was if a filename had the month in text."jan 10, 2017.pdf" etc.

Apologies for opening an old topic.

i also having similar interest. need help to modified the script to change to month-day-year and it wont work.

In function ProduceNewName change line that sets newDate to your desired format.

  • old: var newDate = lpad(year, "0020") + "-" + lpad(month, "00") + "-" + lpad(day, "00");
  • new: var newDate = lpad(month, "00") + "-" + lpad(day, "00") + "-" + lpad(year, "0020");

Thanks, is there anyway to just output YY instead of the YYYY. So mm-dd-yy

you can use substring to remove the first two chars from the year.
In function ProduceNewName change line that sets newDate to your desired format.

  • old: var newDate = lpad(year, "0020") + "-" + lpad(month, "00") + "-" + lpad(day, "00");
  • new: var newDate = lpad(month, "00") + "-" + lpad(day, "00") + "-" + lpad(year, "0020").substring(2,4);
1 Like

Hello all,

awesome thread!

I got a file/folder structure on a network drive with half a million files and folders. A considerable part of the files and folders are (consistently) named as follows:

YYYYMMDD[any character string].[any file extension]

How do I best proceed to rename all (and only those) files and folders corresponding to this naming pattern, so that the result looks like this:

YYYY-MM-DD[any character string].[any file extension]

Probably this is fairly straightforward to do with a RegEx, except that I'm not very fluent in RegEx :man_shrugging:

Also, I am of course grateful for any hints if this renaming might have any negative consequences, apart from a slight lengthening of the file/folder paths (and possibly breaking links and shortcuts).

Thanks very much!

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

I ran into several new problems that I have been trying to solve the past few weeks, but I can't figure out it. Can someone help me modify the script above to avoid changing the following:

I believe it the changes need to be from the second REGEX script.

  • Product B004187
  • Project-35194
  • Project 13-044-2013
  • Project 78215
  • Project82945
  • Project202256
  • Summary Report 1988
  • Summary Report 2016
  • Transmittal 00042
  • X77402-Sample
  • Project - 2022 Report Summary

The script shouldn't modify the items above. Also, if there is a word with a number, it should not convert to date. For dates with (4) numbers, such as 1988, 2016 should be left the same.

  • abcd - 02012018. This one converts to 2002-01-2018 for some reason.

I believe the regex that needs to be corrected is the one below

	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;