Move selected files to sub folder named by parts of the first selected file's name

Hi!

New user here and I can't believe it took me so long to find Directory Opus. It saves me so much time every day! :grinning:

However, I have problems figuring out how to create a button that does the following:
Move all selected files to a new subfolder and names the subfolder with parts of the first selected file's name in the list.

To be more specific what I want to use this for:
I am organizing all personal photos I've take through the years. The images are named Photo_YYYYMMDD_HHmmssXXX.jpg. (XXX=random number to avoid conflict if several images were taken the same second)

I have already put the images in folders matching the dates they were taken.

What I would like to do now in each of these folders is to select all images taken for example between the times 10:00 - 12:00 and press a button that creates a subfolder named 1000 (HHmm - the hour and minute specified in the file name of the first selected file) and move the selected files here.

Is it possible to create a button that executes this?

Thanks in advance!

Try this.

// https://resource.dopus.com/t/move-selected-files-to-sub-folder-named-by-parts-of-the-first-selected-files-name/42706

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

    if (tab.selected_files.count == 0) return;

    cmd.SetDestTab(tab);
    cmd.RunCommand('Copy MOVE CREATEFOLDER=' + tab.selected_files(0).name_stem.replace(/.*_(\d\d\d\d).*/, '$1'));
}
Button as XML
<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none">
	<label>42706</label>
	<icon1>#newcommand</icon1>
	<function type="script">
		<instruction>@script JScript</instruction>
		<instruction>// https://resource.dopus.com/t/move-selected-files-to-sub-folder-named-by-parts-of-the-first-selected-files-name/42706</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.selected_files.count == 0) return;</instruction>
		<instruction />
		<instruction>    cmd.SetDestTab(tab);</instruction>
		<instruction>    cmd.RunCommand(&apos;Copy MOVE CREATEFOLDER=&apos; + tab.selected_files(0).name_stem.replace(/.*_(\d\d\d\d).*/, &apos;$1&apos;));</instruction>
		<instruction>}</instruction>
	</function>
</button>


3 Likes

Hi, Alexander and thanks for you quick reply

I have tried out your suggestion now and it worked beautilfully! :star_struck:

Thank you so much! :balloon::tada: