Go to subfolder in Folder Tree

I have some program and when I set working folder for this program it always creates one subfolder in the working folder, another subfolder in this subfolder and put needed me folders there. Something like:
<working folder>\<subfolder>\<subfolder>\<folder I need>
Folder I need always have same names, however, name of first subfolder is always same too but name of the second subfolder could be different.
I created some kind of script to move folders I need two levels up and delete unneeded subfolders, like this:

SELECT <First folder I need> 
Copy MOVE TO ..\..
SELECT <Second folder I need>
Copy MOVE TO ..\..
Go UP BACK
Go UP BACK
SELECT <Name of first subfolder> SETFOCUS
DELETE

However, to run this I need to go to second subfolder in the Folder Tree each time manually.
It is possible to go to second subfolder with sojme command or script? Like "GO UP BACK" but "Go Down" instead? I unable to set Go Path because name of second subfolder is always different, I need just go two levels down and set focus there.

Please link your account.

Done

Here's a script-button which will find the correct folder (2 levels below the current folder), move it up to the current folder, and (if they are now empty) delete the two intermediate folders.

You will need to edit the top of the script to specify the 1st and 3rd folder names:

	var FirstFolder = "Named SubFolder";
	var NeededFolder = "Needed Folder";

If there is more than one folder under the 1st one, the script will exit without moving or deleting anything. (You could make it display an error dialog if you want.)

Button in .DCF format:

Full script code contained inside the .dcf file, for reference:

function OnClick(clickData)
{
	var FirstFolder = "Named SubFolder";
	var NeededFolder = "Needed Folder";

	var cmd = clickData.func.command;
	cmd.deselect = false; // Leave original selection alone.
	cmd.ClearFiles(); // Ignore original selection.

	// Resolve is important for special folders like /Desktop
	var resolvedSource = DOpus.FSUtil.Resolve(clickData.func.sourcetab.path);

	var pathFirstFolder = DOpus.FSUtil.NewPath(resolvedSource);
	pathFirstFolder.Add(FirstFolder);

	var pathSecondFolder = DOpus.FSUtil.NewPath();
	var gotSecondFolder = false;

	var folderEnum = DOpus.FSUtil.ReadDir(pathFirstFolder);
	while (!folderEnum.complete)
	{
		var folderItem = folderEnum.Next();
		if (folderItem.is_dir)
		{
			if (gotSecondFolder)
			{
				return; // Too many folders.
			}

			gotSecondFolder = true;
			pathSecondFolder.Set(folderItem.RealPath);
		}
	}

	var pathNeededFolder = DOpus.FSUtil.NewPath(pathSecondFolder);
	pathNeededFolder.Add(NeededFolder);

	if (DOpus.FSUtil.GetType(pathNeededFolder) != "dir")
	{
		return; // Needed folder doesn't exist, or it's a file.
	}

//	DOpus.Output(pathNeededFolder);

	cmd.AddLine('Copy MOVE FILE="' + pathNeededFolder + '" TO="' + resolvedSource + '"');
	// Do not remove "NORECYCLE" or the "FAILNOTEMPTY" won't work.
	cmd.AddLine('Delete QUIET NORECYCLE FAILNOTEMPTY FILE="' + pathSecondFolder + '"');
	cmd.AddLine('Delete QUIET NORECYCLE FAILNOTEMPTY FILE="' + pathFirstFolder + '"');
	cmd.Run();
}

Works great, many thanks!
I did some modifications, because I need to move two needed folders and, since they are ALWAYS exist, I removed check for availability. Looks like it works:

function OnClick(clickData)
{
	var FirstFolder = "Named SubFolder";
	var NeededFolder1 = "Needed Folder1";
	var NeededFolder2 = "Needed Folder2";

	var cmd = clickData.func.command;
	cmd.deselect = false; // Leave original selection alone.
	cmd.ClearFiles(); // Ignore original selection.

	// Resolve is important for special folders like /Desktop
	var resolvedSource = DOpus.FSUtil.Resolve(clickData.func.sourcetab.path);

	var pathFirstFolder = DOpus.FSUtil.NewPath(resolvedSource);
	pathFirstFolder.Add(FirstFolder);

	var pathSecondFolder = DOpus.FSUtil.NewPath();
	var gotSecondFolder = false;

	var folderEnum = DOpus.FSUtil.ReadDir(pathFirstFolder);
	while (!folderEnum.complete)
	{
		var folderItem = folderEnum.Next();
		if (folderItem.is_dir)
		{
			if (gotSecondFolder)
			{
				return; // Too many folders.
			}

			gotSecondFolder = true;
			pathSecondFolder.Set(folderItem.RealPath);
		}
	}

	var pathNeededFolder1 = DOpus.FSUtil.NewPath(pathSecondFolder);
	pathNeededFolder1.Add(NeededFolder1);
	var pathNeededFolder2 = DOpus.FSUtil.NewPath(pathSecondFolder);
	pathNeededFolder2.Add(NeededFolder2);

//	DOpus.Output(pathNeededFolder);

	cmd.AddLine('Copy MOVE FILE="' + pathNeededFolder1 + '" TO="' + resolvedSource + '"');
	cmd.AddLine('Copy MOVE FILE="' + pathNeededFolder2 + '" TO="' + resolvedSource + '"');
	// Do not remove "NORECYCLE" or the "FAILNOTEMPTY" won't work.
	cmd.AddLine('Delete QUIET NORECYCLE FAILNOTEMPTY FILE="' + pathSecondFolder + '"');
	cmd.AddLine('Delete QUIET NORECYCLE FAILNOTEMPTY FILE="' + pathFirstFolder + '"');
	cmd.Run();
}
1 Like

I figured I need tags [code] but you was faster :slight_smile: