GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Button: Merge Folders



[ul][li]Alternative version:

Button: Merge Folders v2 is an alternative version of this with slightly different behaviour.


This gives you a button or hotkey which will merge folders together.

For example, you might start with this:

And end up with this:

[li]Using the button:

Select two or more folders and click the button.

It will ask for the name of the new combined folder. By default, it uses the name of the first folder, with the last word removed.

Once you give a name, everything in the selected folders will be moved to the new folder. The old folders will be deleted at the end, as long as they are now empty. (They might not be empty if files in two folders had the same name and you chose to skip them.)

[li]Download and install:

Merge Folders.dcf (3.69 KB)
Download Merge Folders.dcf (above), then go into Settings > Customize Toolbars mode and drag the .dcf file to where you want the new button on your toolbar. Finally, click OK in the Customize window.

[li]The script itself:

The script is provided here, as both text and screenshot, to help people browsing the forum for scripting techniques.

You don’t need to do anything with this code if you just want to use the script.

[code]@script vbscript
Option Explicit

Function OnClick(ByRef ClickData)
Dim FirstDir, NewDir, NewDirEdited, SelDir, re, dlg, cmd, FolderEnum

Set dlg = ClickData.Func.Dlg
Set cmd = ClickData.Func.command
cmd.deselect = False

' Check the right kind of things are selected.

If ClickData.Func.sourcetab.selected_dirs.count < 2 Then
	dlg.Request "At least two folders must be selected", "OK"
	Exit Function
End If
If ClickData.Func.sourcetab.selected_files.count <> 0 Then
	dlg.Request "Only folders should be be selected", "OK"
	Exit Function
End If

' Run a regexp on the first dir's name, for the default new dir name.

FirstDir = ClickData.Func.sourcetab.selected_dirs(0).name
Set re = New RegExp
re.IgnoreCase = True
re.Global = False

re.Pattern = "^(.*) [^ ]+$" ' Strip the last word from the name.
If (re.Test(FirstDir)) Then
	NewDir = re.Replace(FirstDir, "$1")
	NewDir = FirstDir
End If

' Ask the user what to call the new dir.

NewDirEdited = dlg.GetString("Enter new folder name", NewDir, 255, "OK|Cancel", "Merge folders")

If (IsEmpty(NewDirEdited)) Then
	Exit Function
End If

' Move everything in the selected folders into the new dir.

cmd.RunCommand "Copy MOVE ""{filepath$}\*"" TO=""{sourcepath$}"" CREATEFOLDER=""" & NewDirEdited & """"

' Check each selected folder and, if it is now empty, delete it.

For Each SelDir In ClickData.Func.sourcetab.selected_dirs
	Set FolderEnum = DOpus.FSUtil.ReadDir(SelDir, False)
	If (FolderEnum.Complete) Then
		FolderEnum.Close ' So the folder is not locked.
		cmd.AddFile SelDir
		cmd.RunCommand "Delete QUIET"
	End If

End Function[/code]


Button: Merge Folders v2
Copy files from folders to main directory
Merging folders?
Rename from a button
Rename Script - Subscript Out of Range Error

What a great script. Thanks!


Thanks Leo for providing this button, I merge folders regularly, so this is a welcome addition! o)

I mostly need to do full recursive merges though, so I wondered what adjustments would be required to get all files and no subfolders at all.
I came to the conclusion that the copy command may not be enough here, I’d think at least a filter would be required or having a search for files and then move results from the find collection into a single place.

Do you have a recommendation on what weapon to choose? I think I’d prefer something that does not depend on additional configurations (like a filter e.g.).


That’s a pretty open-ended question, in particular how the script would deal with multiple files having the same name.

You’d probably need the script to do everything, unless it used some kludges (maybe copying from flat view or a collection would let you flatten a folder).

DirFlatten and UniqueNames may be of interest.

If you want to talk about this more, please make a thread in Help & Support or the Plugin Development area (which I’ve been thinking of renaming so it covers script coding). It’d be good to keep long discussions about script programming out of threads in the downloads area, so people looking for add-ons can find the end results and concise info about using them.


Love this. Thank you.


Hi there

I trying to get the Merge Folder script to work.

I keep getting the message to select at least 2 folder even if I select all or just 2 folder.

Can you advise me what I am doing wrong.



Please post a screenshot of your Lister (main Opus window) with the folders selected, just before you push the button to run the command. Maybe that will reveal something unexpected.


I have attached screenshot.

Even if a select all folders Ctrl +A I still get the message

Thanks for your help.




I can see something unusual, although I don’t know if it will turn out to be related.

The path the left file display has is C:\Desmond\Music\New Music Library but we can see on the right there is no C:\Desmond folder, unless it is filtered out in some way.

Any idea what’s going on there? Has something non-standard been done to the user profile folder(s)? The path would normally be C:[b]Users[/b]\Desmond.


Hi Leo,
That is just where the music folder is located. Not sure whether this should make a difference
So to test it i copied two folders to the location below and it made no difference


I have a look at the code

If ClickData.Func.sourcetab.selected_dirs.count < 2 Then
dlg.Request “At least two folders must be selected”, "OK"
Exit Function
End If

and it appears the problem is that it is not trapping that 2 folders have been selected. Is there something that could be preventing

either the variable not being set correctly or the functions is not working.

Thanks again for your help.




I just installed the button and it worked like a charm. Thanks Leo. :thumbsup:


Just seemed odd, and maybe a hint about what’s going wrong, that the folder shown on the left does not seem to exist according to the right, in the screenshot. Maybe something else is going on there, though.

How are you launching the script/button?

What happens if you use this one?:

[code]@script vbscript
Option Explicit

Function OnClick(ByRef ClickData)
Dim str
str = "Path = " & ClickData.Func.sourcetab.path
str = str & vbCRLF & "Collection Dirs = " & ClickData.Func.sourcetab.selected_dirs.count & " / " & ClickData.Func.sourcetab.dirs.count
str = str & vbCRLF & "Stat Dirs = " & ClickData.Func.sourcetab.stats.seldirs & " / " & ClickData.Func.sourcetab.stats.dirs
str = str & vbCRLF & "SelStat Dirs = " & ClickData.Func.sourcetab.selstats.seldirs & " / " & ClickData.Func.sourcetab.selstats.dirs
str = str & vbCRLF & "Collection Files = " & ClickData.Func.sourcetab.selected_files.count & " / " & ClickData.Func.sourcetab.files.count
str = str & vbCRLF & "Stat Files = " & ClickData.Func.sourcetab.stats.selfiles & " / " & ClickData.Func.sourcetab.stats.files
str = str & vbCRLF & "SelStat Files = " & ClickData.Func.sourcetab.selstats.selfiles & " / " & ClickData.Func.sourcetab.selstats.files

ClickData.Func.Dlg.Request str, "OK", "Test results"

End Function[/code]


Hi Leo,

I am starting the script via Menu_playful_150218 Folder Merge Folder option. I previous poster indicated that he is using a button of some sort.

I am starting it the correct way.

If not could you possible point me in the right direction

Have you got instruction of how to install the code you have posted please.




Menu_playful_150218 isn’t one of the standard toolbars. I’m guessing it is from Playful’s tutorials. I’m not familiar with it or how you are using it. Is it a toolbar that is part of the Opus window, or is it floating on the screen as a separate window?

Here’s the test script as a .dcf file. (Instructions on how to drag it to your toolbar are in the root post. Please put it next to the button or menu item you’re using so it is run and tested in the same way.)
Script.dcf (682 Bytes)


Hi Leo,

Thanks for all your help.

Attached is the results of the test script.

When I run (Script.dcf with the 8 on the button) it did not appear to do anything.

Thanks again



Hi Leo,

I don’t know if this is relevant but I just noticed that when the Merge folders script is run it first seems to deselect the selected folders.




You’ve done the test in a different folder to before but, if the scenarios are the same, that shows that scripts do see 2 folders are selected, so it doesn’t make sense if the other script is thinking less than 2 folders are detected if run in the same way and same situation.

I still think something strange is going on in your earlier screenshot where it shows the path is C:\Desmond and not C:\Users\Desmond.


Nice one, Leo - thanks. Very handy.



Hey Leo,
thank you very much for this!

Is it possible to create such a script with files instead of folders?


Updated and enhanced version here. Thanks leo for the original.