Help on button: make folder junction and set the name

I want to make a button to create a folder junction. At the moment I have a simple one:

@nodeselect Copy MAKELINK=junction Set FOCUS=Dest
What I would like the button to do is: I select a folder in the source tab and select a folder (or a previous junction, there should be no difference, right?) in the destination tab. Then when I press a button:

  1. Delete the folder/junction that is selected in the destination tab, but remember the name of that folder/junction.
  2. Either:
    Create a new junction from selected in source folder to destination. Then rename that junction to previously deleted folder/old junction name.
    Or, create new junction in the destination already using the deleted folder/old junction name.

Basically replace the selected folder in the destination tab with a junction to the selected folder in the source tab, keeping the old name. Or same thing, just replace old junction with a new one.

I hope I'm making sense. I need to do this many many times every day and it would be so much a time saver. Now every time I need to do it by hand: copy old name, delete, create junction, rename...

Thanks in advance.

Are you using Opus 11, or still on 10? (Profile says 10, but many people don't update it.)

If you're on Opus 11 then this should be possible using scripting, but a script won't work with 10.

Thanks for the quick reply, leo. Sorry, even forgot to change the version in profile. Using 11.15. Funnily enough, the seems to be version 11.16 out, but Dopus says that my version 11.15 is the current version :slight_smile:

I would very much appreciate if you could script it for me. I know very little vbscript and no javascript at all.

Sorry for taking so long.

Please give this a try with test source and destinations area, to make sure it works as you expect, before letting it near any real folders.

I made it so it will only run if the left side is the source, to avoid any nasty accidents if you select the left and then the right. You can modify that if you wish, but it seemed a good precaution.

To use it, select exactly one folder on the right to be replaced, and exactly one folder on the left (source) that will be pointed to. Click the button and it will delete the right folder (prompting for confirmation if you have Opus configured to; that can be suppressed but seems a good precaution), and then create a junction with the same name as it that points to the left folder.

If you want to do away with the delete confirmation, or the left=source check, you could probably make it safe by adding some logic so that it checks the thing it is replacing is already a junction and thus there won't be any data loss if it is deleted. (Assuming that you'll always be replacing one junction with another of the same name but pointing to another place. Maybe I'm wrong that that's how you plan to use it, but if not it might be a good check to add.)

[code]@nodeselect
@script vbscript
Function OnClick(ByRef clickData)

Set fsu = DOpus.FSUtil
Set cmd = clickData.func.command
cmd.deselect = False

If (clickData.func.sourcetab.lister.dual = 0) Then
	Exit Function
End If

' Only work left-to-right, to avoid accidents.
If (clickData.func.sourcetab.right) Then
	Exit Function
End If

If (clickData.func.sourcetab.selected_files.count <> 0 _
Or  clickData.func.sourcetab.selected_dirs.count <> 1 _
Or  clickData.func.desttab.selected_files.count <> 0 _
Or  clickData.func.desttab.selected_dirs.count <> 1) Then
	Exit Function
End If

Set SourcePath = fsu.NewPath(clickData.func.sourcetab.selected_dirs(0).realpath)
Set DestPath = fsu.NewPath(clickData.func.desttab.selected_dirs(0).realpath)
If (Not DestPath.test_parent) Then
	' Unexpected. Could probably work around but can't think of valid scenario where it'd happen.
	Exit Function
End If

cmd.ClearFiles
cmd.AddFile DestPath
cmd.RunCommand "Delete"
If (cmd.results.result = 0) Then
	Exit Function
End If

cmd.ClearFiles
cmd.AddFile SourcePath
cmd.RunCommand "Copy MAKELINK=junction TO=""" & DestPath.pathpart & """ AS=""" & DestPath.filepart & """"

End Function[/code]

OMG, leo, thank you thank you, thank you.
I was pretty sure the topic was dead and over. Imagine my suprise. Youre awesome, man, like always.

The script works like a charm. I just switched the sides, from right to left. The monitor I'm doing this on is to my left, so the right side is more convenient to work on (more to the center). Just today, it saved me probably an hour of tedious copy/delete/makejunction/paste.

Thanks again.