Copy timestamp based on oldest modified date within selection

The reason behind it is a long, uninteresting, story.
I needed to split large PDF files into smaller ones. For instance FileXYZ.pdf (500MB) into 10 parts, like FileXYZ-part1.pdf .. part2.pdf etc.
Obviously the modified timestamp of these part1, part2 files are the same as the created timestamp.

Right now I use
"ClipboardEx COPYTIMESTAMPS" and
"ClipboardEx PASTETIMESTAMPS=modified"
to copy the timestamp of the 'old' file onto the new ones.

This is fine for a limited number of files, but I've to do some 250 files.

I wonder if it would be possible to:
select the old, large pdf
and the new files
and change the modified date of the new ones, based on the oldest modified date within the selection.

It would faster, less clicks especially when assigning a hotkey combination to this action.

SnagIt-07092020 093433

Here's a button which should do that.

Test it in a new folder on some dummy files first to make sure it works how you want it to.

Modified to Oldest.dcf (4.6 KB)

Script code for reference:

function OnClick(clickData)
 {
    var cmd = clickData.func.command;
    cmd.deselect = false;

	// Change to .selected to do files and folders (untested), not just files (tested).
	var selected = clickData.func.sourcetab.selected_files;

	var oldestItem = null;

    for (var files = new Enumerator(selected); !files.atEnd(); files.moveNext())
	{
        var item = files.item();

		if (oldestItem == null || oldestItem.modify.Compare(item.modify) > 0)
		{
			oldestItem = item;
		}
	}

	if (oldestItem == null)
	{
		return;
	}

	var anyFiles = false;
	cmd.ClearFiles();

    for (var files = new Enumerator(selected); !files.atEnd(); files.moveNext())
	{
        var item = files.item();

		if (oldestItem != item)
		{
			cmd.AddFile(item);
			anyFiles = true;
		}
	}

	if (!anyFiles)
	{
		return;
	}

	var timeStr = DateTimeForCmd(oldestItem.modify);
	var cmdLine = 'SetAttr MODIFIED="' + timeStr + '"';
	cmd.RunCommand(cmdLine);
}

function ZeroPad(s,c)
{
	s = s + "";
	while(s.length < c)
	{
		s = "0" + s;
	}
	return s;
}

function DateTimeForCmd(d)
{
	var year, month, day, hour, minute, second;

	if (DOpus.TypeOf(d) == "object.Date")
	{
		// Opus date object
		year   = d.year;
		month  = d.month;
		day    = d.day;
		hour   = d.hour;
		minute = d.min;
		second = d.sec;
	}
	else
	{
		// Javascript date object
		year   = d.getFullYear();
		month  = d.getMonth() + 1;
		day    = d.getDate();
		hour   = d.getHours();
		minute = d.getMinutes();
		second = d.getSeconds();
	}

	year   = ZeroPad(year,   4);
	month  = ZeroPad(month,  2);
	day    = ZeroPad(day,    2);
	hour   = ZeroPad(hour,   2);
	minute = ZeroPad(minute, 2);
	second = ZeroPad(second, 2);

	return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
}
1 Like

THANK you very much indeed!
This is really magnificent.
It saves a lot of boring clicks -and- time.
I knew it was a strange request, a 'long shot', so to say and I honestly didn't expect a solution.

Many thanks again.

1 Like

Please allow me to get back on this old thread. I use the above quite regularly: stored it under a Function key. I wonder, would it be possible to 'expand' the script so it will use a folder date as oldest source.

It works fine on files, but not when using folder file combination.

Drop it, when it is too complex. Just asking.

Thanks.

See the comment at the top of the script.