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
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).
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?
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.
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 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;