Get dates from filenames of selection and move into new folder with the date as name

I need help with creating a new script or rather combining existing functions to do what I described in the title. Since I am fairly new to Directory Opus and the forum I don't know how to link other threads, yet. But I have been using following functions:

Changing Filename date format to yyyy-mm-dd

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;
}

and

@nofilenamequoting
=return "Copy MOVE HERE CREATEFOLDER="+Left(Trim(file), 4)
@nofilenamequoting
=return "Copy MOVE HERE CREATEFOLDER="+Left(Trim(file), 6)

Is there a way to combine those to get the dates from the filenames of the selection, create new folders with the format YYYY-MM-DD from the filenames and move them into the folders?

Never tested in a rename script, but it the Rename interface if you rename to "SomeFolder\Somefile" it is creating the folder as named puts the files into it.
So I guess that adapting the rename script so that it prepends the new formatted date plus "\" befort the new name, it should work.

EDIT : again not tested but something like, in ProduceNewName :

 return newDate + "\\" + oldname.replace(matched, newDate);
1 Like

I just noticed that the button also changes the date format in the file name. Is there a way to keep the files names as they are?
Right now a file named "220405 File" gets moved into a new folder named "2022-04-05" but its name is also changed into "2022-04-05 File"

Without getting into too many details, your rename script does the following :

  • Its entry point is OnGetNewName which gets the file name as an input (getNewNameData).
  • Then it applies a regular expression to extract what should be the date in the filename, and gets separate values for the numbers representing the day, the month and the year.
  • Each gets stored in the array regexMatch if the regex execution is successful.
  • Then the function ProduceNewName is called to produce the new name.

ProduceNewName does the following :

  • var newDate = lpad(year, "0020") + "-" + lpad(month, "00") + "-" + lpad(day, "00"); : creates a string variable newDate that contains the date reformatted as intended.
  • DOpus.OutputString("'" + oldname +"' - '" + matched +"' - '" + newDate +"' day:'" + day +"' month:'" + month + "' year:'"+year+"'"); : just an output of the different variables values. No effect
  • return newDate + "\\" + oldname.replace(matched, newDate); : gives the name you want the file to have after renaming which is now the concatenation of the following :
    • newDate + "\\" : Prepends the new formated date with \\ which will lead to putting the file in a folder named according to this newly formatted date
    • oldname.replace(matched, newDate) : original name in which you replace the date part that matched the regular expression in OnGetNewName (which is the date with its original format) by the date in the new format.

All in all, if you want to preserve original name, you just have to remove the replace in the return part of ProduceNewName :

return newDate + "\\" + oldname;
2 Likes

Thank you for the detailed breakdown with the solution! I am still learning and this helped a ton.