GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Rename Script - Subscript Out of Range Error

I'm writing a rename script that moves selected files to either a pre-defined location for each file or pops up a dialog to select the destination if its not pre-defined.

I have dim'ed and initialised/set (where necessary) the variables etc and I am using the following line of code to open the folder selection dialog (parameters are defined):

ret = dlg.folder(dlg.title,strPath,1,dlg.Window)

If I select a folder from the dialog, all is well (the selected folder path is returned). However if I select 'Cancel' then I get this error (the line above is on line 48 and tab indented twice):

Error at line 48, position 3
Subscript out of range (0x800a0009)

I've been through the help file seeking the answer as well various sites returned by Google with no joy other than informing me that the returned object is out of range!

Any suggestions please?

Feeding dlg.title and dlg.WIndow back into the same dlg object is a little odd, but I'm not sure if it's what's causing the problem.

Can you show us the full script?

This is the 'work in progress' script. Output is to the logging window only.

[code]@script vbscript
Option Explicit

Function OnGetNewName(ByRef GetNewNameData)

Dim item, strPath, strFolder, strStem, strVer, Meta, intVerExists, dlg, strFile, ret

Set Item = GetNewNameData.item
Set Meta = Item.MetaData.exe
Set dlg = DOpus.Dlg

' default path to storage location
strPath = ("\\MEDIASERVER\Resources\PC Hard-Software\Software\")

'get the file version from metadat
strVer = Meta.prodversion

intVerExists = InStr(Item.name_stem,Left(strVer,5)) 'Returns start position of version if found

' resolve whether version string is correct or not present		
If intVerExists = 0 Then 
	If strVer = "0.0.0.0" Then
		strVer = ""
	Else
		strVer = "_" & strVer
	End If
Else
	If intVerExists > 0 Then
		strVer = ""
	End If
End If

' build new file name to include version if required
strFile = Item.name_stem  & strVer & item.ext

'identify each file using first 3 letters (should be unique)
strStem = Left(item.name_stem,3)

'set the folder for pre-defined files
Select Case strStem
	Case "cal" strFolder = strPath & "Calibre\" & strFile
	Case "cdb" strFolder = strPath & "CDBXP\" & strFile
	Case "DOp" strFolder = strPath & "DOpus\V11\" & strFile
	Case "Dro" strFolder = strPath & "Dropbox\" & strFile
	Case "Med" strFolder = strPath & "MediaMonkey\" & strFile
	Case "Goo" strFolder = strPath & "GoodSync\V9\" & strFile
	Case "iTu" strFolder = strPath & "Apple\" & strFile
	Case "Tea" strFolder = strPath & "TeamViewer\" & strFile
	Case Else
	
	' or Show a dialog to select destination folder on server and add filename to the path
	ret = dlg.folder("Folder for " & item.name_stem,strPath,1)
	If ret = "" Then
		DOpus.Output("Folder selection cancelled")
	Else
		strFolder = ret & "\" & item.name
	End If
	
End Select

DOpus.Output(strFolder)

'OnGetNewName = strFolder

End Function
[/code]

Actually that's a slightly earlier version before I defined dlg.window and dlg.title but the result is still the same.

Thanks.

On success, dlg.folder returns a Path object, rather than a string. It is implicitly turned into a string if you use "ret = dlg.folder" instead of "Set ret = dlg.folder", but that implicit conversion will fail if the path is invalid because the dialog was cancelled.

The object has a result property which lets you check if the dialog was cancelled, as per the docs:

Here's some sample code showing how to check the result property:

Set ret = dlg.folder("Select folder","C:\",1) If ret.result = False Then DOpus.Output("Folder selection cancelled") Else strFolder = ret End If

1 Like

Thanks Leo. I'll give that a try.

Although I have completed my script by building a dialog box from scratch that puts all the folders in the destination location into a drop-down list and that seems to work OK as well.

I'll try and tidy it all up and then maybe offer it to the masses :slight_smile:

Thanks again.

One general suggestion: It might be better to use a normal script rather than a rename script for this. I don't think the rename command can move files across devices. Rename scripts also don't have access to the lister window property so, if they show dialogs, the dialog will not have its parent window set properly.

So I'd do this slightly differently, in a button script. You can loop through the selected files, and for each one run a command which moves them to a determined folder. My Merge Folders script has most of the building blocks you'll need. It moves everything to a single folder, which isn't what you want, but just after that it loops through the selected folders and runs a delete command on the empty ones, which shows how you can loop through the selected items and run commands on them individually. The command object you get has all selected items attached to it initially, which is why the RunCommand Copy line affects all files, but you can also call cmd.ClearFiles and then add individual items to run commands that only affect them, as it does after that.

[quote="leo"]One general suggestion: It might be better to use a normal script rather than a rename script for this. I don't think the rename command can move files across devices. ...

So I'd do this slightly differently, in a button script....
[/quote]
My script as is moves files from my main PC to my server OK and I have converted it to a button script - correctly or otherwise - and it works. I will certainly look at your Merge Folders script and learn from it.

I'm slowly learning DOpus scripting (I've used VBA somewhat so understand the basics but need to get my head around the DOpus specifics) and thought my project would be a useful place to start.

Thank you for your support and suggestions.