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 |
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
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).
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:
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;
}
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;
If the date at the start of the filename exists in the metadata, how about...
Thanks for this idea, which for some reason I only just saw now.
In our case, this would be too risky because the files are edited and shifted around all the time, including to SharePoint, OneDrive, E-Mail etc.
Therefore, using metadata would not make sure to absolutely keep the date that was in the original file name. Also, the date in the original file name was entered manually by people in most cases. So it might not even conform to the modified or created date of the original file.
I just tried this with great success on both files and folders! Thank you very much again.
Just for better understanding, why do we use the fourth part in the search string [(.*)] and in the replace string [\4]?
I believe the fourth part is for the file extension, which we ignore anyway (and which is not even present with folders). So is there a certain reason that we need this part?
No!
Indeed!
Indeed!
In RegEx mode, the entire expression has to match. So if we removed it, we could only handle filenames that consist of eight digits.
We could use RegEx-and-Replace mode and remove both (.*)
and \4
for the same effect as the original regex. Try it out!
I see, in case of a RegEx the pattern (.*) simply catches a string with zero to any number of any characters and has nothing to do whatsoever with a file extension? (which we ignore anyway)
simply catches a string with zero to any number of any characters and has nothing to do whatsoever with a file extension?
Yes.