How to retrieve the modification date of a selected file in a command line?

My use case is as follows:

I would like to organize my photos. Generally, I select several pictures and then move them to a folder. The naming format is "date + description,"

for example: 20240618 Going out for hotpot with family.

Currently, I am able to achieve that after selecting the photos, by pressing a button, a window pops up allowing me to enter the file name. Upon confirmation, a folder is automatically created with the entered name, and the selected files are moved into this newly created folder.

The internal command I have written so far is as follows:

Copy MOVE TO HERE CREATEFOLDER COPYFILETIMES=all

The difficulties I’m currently facing are:

  1. I don’t know how to pre-fill content when the create folder window pops up (a window appears asking for a name when creating a folder).

  2. I don't know how to retrieve the "modified date" of the first file among multiple selected files (which is actually the photo's shooting time, and may involve reading EXIF information).

Specific functionality I want to achieve:

Essentially, what I'm trying to do is have the program automatically determine the modified date of the first selected file when I've chosen several photos, then automatically fill that into the create folder name dialog. All I need to do is input a theme—which represents these photos that I'd like to organize into one folder—and I can complete sorting and archiving several pictures.

To add some details that I am aware of:

  1. In my batch renaming module, there’s a command like {modifieddate|D#yyyyMMdd} available which retrieves the photo's shooting date (the modified date) for renaming purposes. However, this command seems not to be effective within internal commands.

  2. At present, I'm implementing this by creating a new button in the toolbar through which I'll write commands.

1 Like

You could use a command like

Rename TO={modifieddate|D#yyyyMMdd}\*

but for your specific case you'll need a script like this:

// https://resource.dopus.com/t/how-to-retrieve-the-modification-date-of-a-selected-file-in-a-command-line/52498

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

    if (!tab) return;
    if (!tab.path) return;
    if (!tab.selected_files.count) return;

    cmd.SetDest(tab.path);

    var cmdLine = 'Copy' +
        ' MOVE' +
        ' COPYFILETIMES=all' +
        ' CREATEFOLDER="{dlgstring|Enter folder name:|' + tab.selected_files(0).modify.Format('D#yyyyMMdd') + ' }"';

    // DOpus.Output(cmdLine);
    cmd.RunCommand(cmdLine);
}
XML
<?xml version="1.0"?>
<button backcol="none" display="label" hotkey_label="yes" label_pos="right" textcol="none">
	<label>52498</label>
	<tip>how-to-retrieve-the-modification-date-of-a-selected-file-in-a-command-line</tip>
	<icon1>#script</icon1>
	<function type="script">
		<instruction>@script JScript</instruction>
		<instruction>// https://resource.dopus.com/t/how-to-retrieve-the-modification-date-of-a-selected-file-in-a-command-line/52498</instruction>
		<instruction />
		<instruction>function OnClick(clickData) {</instruction>
		<instruction>    var cmd = clickData.func.command;</instruction>
		<instruction>    var tab = clickData.func.sourcetab;</instruction>
		<instruction>    cmd.deselect = false;</instruction>
		<instruction />
		<instruction>    if (!tab) return;</instruction>
		<instruction>    if (!tab.path) return;</instruction>
		<instruction>    if (!tab.selected_files.count) return;</instruction>
		<instruction />
		<instruction>    cmd.SetDest(tab.path);</instruction>
		<instruction />
		<instruction>    var cmdLine = &apos;Copy&apos; +</instruction>
		<instruction>        &apos; MOVE&apos; +</instruction>
		<instruction>        &apos; COPYFILETIMES=all&apos; +</instruction>
		<instruction>        &apos; CREATEFOLDER=&quot;{dlgstring|Enter folder name:|&apos; + tab.selected_files(0).modify.Format(&apos;D#yyyyMMdd&apos;) + &apos; }&quot;&apos;;</instruction>
		<instruction />
		<instruction>    // DOpus.Output(cmdLine);</instruction>
		<instruction>    cmd.RunCommand(cmdLine);</instruction>
		<instruction>}</instruction>
	</function>
</button>

How to use buttons and scripts from this forum

KWS_Work,
As a professional photo organizer, I do not recommend you use the File Modified date.
Use Date Taken if the photos do have Exif Metadata.

How should this be implemented in specific operations?

Sorry, I didn't expect to get a response so quickly, thank you for the detailed answer!!