Button: copy to last folder (path)

I would like to create a simple button that copies the selected files to the last folder (path).

Thank you very much for helping!

"Last folder" as in the one you would go to if you clicked the Back button? Or something different?

Last folder: The last folder, into which I copied some files, for example C:\pictures

So if I am then working in a directory like C:\actual I would like to select some files there and the "copy to last folder" button should then initiate the copy process from C:\actual to C:\pictures

As an addition I could imagine that a few buttons with a fixed "copy to" path could also be helpful, for example "copy to C:\project A" or "copy to C:\project B" as 'hardcoded" paths

That's a bit tricky, you'd need to keep records when copying.

Checkout this for some inspiration.

That's Opus 101, go for it!

(You'd be embarrassed if somebody told you how to do them)

Is a drop-down history list what you want?
image

The "tricky" thing seems too complicated for me at first glance. I did not code anything for DirOpus, yet. And currently I am very busy with moving to another house...
I would need some step-by-step instructions where to copy what and what to do to get it working. Seems to be exactly what I want, though.

And it seems that I am not the only person with the desire to be able to copy to the last destination folder again and again :slight_smile:

This could also be very helpful, especially if I could assign a hotkey to the 1st, 2nd position of that list so that I do not have to use this menu all the time, but could use also a hotkey directly after selecting the files that should be copied

If the "last folder" you said is the last copied target, then you can store it in a variable or file.

Script Add-ins

Command: copyto

//-- Add string trim() method
String.prototype.trim = function(s){s=s||"\\s";return this.replace(new RegExp("^("+s+"){1,}\|("+s+"){1,}$","g"),"");}

scriptDesc = "Copy to... (Path & Folder name history drop-down list)";

function OnInit(initData) {
    initData.name = "Copy to... (Path & Folder name history drop-down list)";
    initData.version = "1.0 (2024-04-26)";
    initData.copyright = "(c) 2023 Ken";
	initData.url = "https://resource.dopus.com/t/button-copy-to-last-folder-path/50497/7";
    initData.desc = scriptDesc;
    initData.default_enable = true;
    initData.min_version = "12.0";

    var cmd = initData.AddCommand();
    cmd.name = "copyto";
    cmd.method = "OnCopyto";
    cmd.desc = scriptDesc;
    cmd.label = "Copy to... (Path & Folder name history drop-down list)";
    cmd.template = "";
    cmd.hide = false;
    cmd.icon = 'script';
}



function OnCopyto(scriptCmdData) {
    var cmd = scriptCmdData.func.command;
	var tab = scriptCmdData.func.sourcetab;
	var sourcepath = tab.path;
	var selectedItems = tab.selected;
	if (selectedItems.count == 0)
	{
		cmd.RunCommand('CreateFolder READAUTO=no');
	}
	else
	{
    
	//var focusI = tab.GetFocusItem.name_stem;                                   // Get the base name of the item in focus
    
	var dlg = scriptCmdData.func.Dlg();
    dlg.title = "Directory Opus";                                              // Title
    dlg.template = "dlgCombox";
	dlg.detach = true;
    dlg.Create();
	dlg.Control("static").style = "b";
	dlg.Control("static").label = "Copy to... (Path && Folder name history drop-down list)";                // Topic
	dlg.Control("static2").label = "Select or enter folder name or path";      // Notes
	dlg.Control("static3").label = "#0:#copy";                                 // Icon
	
	
    var filePath = "%appdata%\\GPSoftware\\Directory Opus\\Scripts\\INI\\Copyto...History.ini";            // File path
	
    if(DOpus.FSUtil.Exists(filePath)) {                                        // Read file
	   var FSU = DOpus.FSUtil;
       var ST = DOpus.Create.StringTools();
       var text = FSU.OpenFile(filePath).Read();
       var text = ST.Decode(text, "utf-8 bom");
    } else {var text = ""}
    
    var myArray = text.split("\r\n");
    
	dlg.Control("combo").label = myArray[0];                                   // Defalut value
	dlg.Control("combo").SelectRange(0, -1);                                   // Select defalut value
	
    for(i = 0; myArray[i]; i++) {
		dlg.Control("combo").AddItem(myArray[i]);                              // Add each item to drop-down list
	}
    
    dlg.Show();

// msg loop	
	do
	{
		msg = dlg.GetMsg();
/*
        // Selection
		if(msg == true && dlg.Control("combo").focus == true) {
		DOpus.Output(dlg.Control("combo").label);
		}
*/
        // Result, perform the move operation
		if (dlg.result == 1) {
		    var newSubfolder = dlg.Control("combo").label.trim();
		    if (newSubfolder == "")
				return
		    newPath = sourcepath + "\\" + newSubfolder;
		    if (DOpus.FSUtil.Exists(newPath)) {
				cmd.ClearFiles();
                for(var e = new Enumerator(selectedItems); !e.atEnd(); e.moveNext()) {   // If the folder exists, do not move the folder itself
	                eItem = e.item() + "";
					if (eItem != newPath)
						cmd.AddFile(eItem)
				}
			}
		    if (newSubfolder.match(/:\\/) || newSubfolder.match(/\\\\/)) {
			    cmd.RunCommand('Copy TO CREATEFOLDER="' + newSubfolder + '"');           // If it is a path, create the path and move the selected items into it
		    } else {
		        cmd.RunCommand('Copy HERE CREATEFOLDER="' + newSubfolder + '"');         // Otherwise create a folder and move the selected items into it
		    }
			saveHistory(text, newSubfolder)
			DOpus.Notify("Copy to...", "Done!")
		}
/*		
		// If the button 3 is pressed
		if(dlg.result == 2) {
		DOpus.Output("Button 3 is pressed");
		}
		
		// If the button 4 is pressed
		if(dlg.result == 3) {
		DOpus.Output("Button 4 is pressed");
		}
*/
	} while (msg == true);
   }
}


function saveHistory(text, newSubfolder)                                       // Save history
{
	newHistoryList = newSubfolder + "\r\n";
	var textArray = text.split("\r\n");
	
    for(i = 0; textArray[i]; i++) {
		if(textArray[i] != newSubfolder)
		{
		   newHistoryList = newHistoryList + textArray[i] + "\r\n"
		}
		if(i == 15)
		   break
	    }
		//DOpus.Output(newHistoryList)
	newHistoryList = newHistoryList.trim();
	
	var FSU = DOpus.FSUtil;
    var ST = DOpus.Create.StringTools();
	var filePath = "%appdata%\\GPSoftware\\Directory Opus\\Scripts\\INI\\Copyto...History.ini";   // File path
	var ofile = FSU.OpenFile(filePath, "wa");
    var utf8bom = ST.Encode(newHistoryList, "utf-8 bom");                      // Decode as UTF-8 with BOM
    if(ofile.error == 0)
    {
       ofile.Write(utf8bom)                                                    // Write history to file
    }
    ofile.Close()
}



==SCRIPT RESOURCES
<resources>
	<resource name="dlgCombox" type="dialog">
		<dialog fontsize="8" height="78" lang="english" resize="yes" title="Directory Opus" width="322">
			<control edit="yes" height="40" name="combo" type="combo" width="306" x="8" y="40" />
			<control close="3" enable="no" height="14" name="btn4" resize="xy" title="&amp;4" type="button" visible="no" width="50" x="101" y="57" />
			<control close="2" enable="no" height="14" name="btn3" resize="xy" title="&amp;3" type="button" visible="no" width="50" x="155" y="57" />
			<control halign="left" height="8" name="static" title="Topic" type="static" valign="top" width="280" x="30" y="10" />
			<control halign="left" height="16" name="static2" title="Notes" type="static" valign="top" width="280" x="30" y="22" />
			<control halign="center" height="14" image="yes" name="static3" title="Icon" type="static" valign="top" width="14" x="8" y="7" />
			<control close="1" default="yes" height="14" name="btnOK" resize="xy" title="&amp;OK" type="button" width="50" x="209" y="57" />
			<control close="0" height="14" name="btnCancel" resize="xy" title="&amp;Cancel" type="button" width="50" x="263" y="57" />
		</dialog>
	</resource>
</resources>

Button or Hotkey

Copy to last path

// Copy to last path

function OnClick(clickData)
{
    var filePath = "%appdata%\\GPSoftware\\Directory Opus\\Scripts\\INI\\Copyto...History.ini";            // File path

    if (DOpus.FSUtil.Exists(filePath)) {                                        // Read file
		var FSU = DOpus.FSUtil;
		var ST = DOpus.Create.StringTools();
		var text = FSU.OpenFile(filePath).Read();
		var text = ST.Decode(text, "utf-8 bom");
		var textArr = text.split("\r\n");
		var last = textArr[0], penult = "";
		if (textArr.length) penult = textArr[1]
    } else return

    var cmd = clickData.func.command;
    if (last) {
        if (last.match(/:\\/) || last.match(/\\\\/)) {
            cmd.RunCommand('Copy TO CREATEFOLDER="' + last + '"');           // If it is a path, create the path and move the selected items into it
        } else {
            cmd.RunCommand('Copy HERE CREATEFOLDER="' + last + '"');         // Otherwise create a folder and move the selected items into it
        }
		DOpus.Notify("Copy to last path", "Done!")
	}
}

Copy to penult path

// Copy to penult path

function OnClick(clickData)
{
    var filePath = "%appdata%\\GPSoftware\\Directory Opus\\Scripts\\INI\\Copyto...History.ini";            // File path

    if (DOpus.FSUtil.Exists(filePath)) {                                        // Read file
		var FSU = DOpus.FSUtil;
		var ST = DOpus.Create.StringTools();
		var text = FSU.OpenFile(filePath).Read();
		var text = ST.Decode(text, "utf-8 bom");
		var textArr = text.split("\r\n");
		var last = textArr[0], penult = "";
		if (textArr.length) penult = textArr[1]
    } else return

    var cmd = clickData.func.command;
    if (penult) {
        if (penult.match(/:\\/) || penult.match(/\\\\/)) {
            cmd.RunCommand('Copy TO CREATEFOLDER="' + penult + '"');           // If it is a path, create the path and move the selected items into it
        } else {
            cmd.RunCommand('Copy HERE CREATEFOLDER="' + penult + '"');         // Otherwise create a folder and move the selected items into it
        }
		DOpus.Notify("Copy to penult path", "Done!")
	}
}

Thank you very much for taking on my problem.

Please let me know what to do with the code. I tried to start with the simple version hotkey for last folder. I copied the script into a button like this, but it does not seem to work:

Is my proceeding correct? Is there anything missing?

Before that I used the DirOpus copy command to copy three files to a "last folder" folder to be sure there is a last path.

You need to use the first script, which will save the path to "%appdata%\GPSoftware\Directory Opus\Scripts\INI\Copyto...History.ini".
(Or add code in the copy button to save target path)