Enumerating a folder problem VB

The VB Code here is supposed to take a selected file and rename it into a storage directory.

The renamed files use the same name stem and the script adds a number to make the filename unique.

The script cycles through the destination directory to pick up the name of the last file, strips out all characters apart from the number and then constructs the name of the file to be renamed.

My problem is that it works simply fine but only once. After that the FSutil part of the script refuses to recognise the file I have just renamed into the directory and I get a filename exists message.

Can any of you VB gurus point me in the direction of a solution to this problem and tell me what I am doing wrong?

My feeling is that the script was working fine in earlier iterations of Opus 12, but somewhere along the upgrade path things started to go astray. As sometimes I do not use the script for months and then use it many times in a week, I can't tell in which version it stated to go wrong.


Function OnClick(ByRef clickData)
clipString = ""
   For Each SelectedItem In ClickData.Func.sourcetab.Selected
         ClipString = SelectedItem.name_stem
		 exto = SelectedItem.ext
		 pathbo = selectedItem.path & "\" & ClipString & exto
		 oldpath = SelectedItem.path & "\" 
		 totfile = clipstring +  exto
If clickData.func.sourcetab.selected_files.count = 0 Then
      Msgbox "Yoy MUST select a file to move",17, "NO FILE SELECTED"
	  End If
   	  If clickdata.func.sourcetab.selected_files.count >1 Then
	  Msgbox "You can only rename one file at a time",16,"FILE SELECTION ERROR"
	  exit function
	  End If
	Dim List1, folderItem,	folderEnum, tagstring, dopple, clipstring, selecteditem, exto, pathbo, folderdir, totchar,whichform,sider,bottomer,piccydata,thisdir,dlg,msg,thisisit,retval,itemno
'Calculate the next number needed to rename the image
thisdir ="E:\Imaging\Working Images\Glamour"
	Set folderEnum = DOpus.FSUtil.ReadDir("E:\Imaging\Working Images\Glamour", False)
	Do While (Not folderEnum.Complete)
		Set folderItem = folderEnum.Next
		totchar = Len(folderitem)
	    folderdir = Right(folderitem,(totchar-46))
		itemno =left(folderdir,5)
		itemno = itemno + 1
itemno = CStr(itemno)
folderdir = thisdir + "\GLAMG_Mask_"  + itemno + ".jpg"
totfile = thisdir + "\" + totfile
msgbox dopple
cmdline = "Properties SETLABEL=PA,PB,PC"
		ClickData.Func.Command.AddLine("Copy MOVE TO """ & thisdir & """")
 		ClickData.Func.Command.AddLine("RENAME FROM """& totfile & """ TO """ & folderdir& """")
End Function

Have you tried debugging it? Add some DOpus.Output lines into the loop. My guess is your Right(...-46) and Left(...5) stuff is not working with all of your filenames.

If that isn't it, try simplifying the code so it only does the thing you think is failing, removing everything else. Doing that usually reveals the problem on the way, and if it doesn't it will make it easier for other people to spot it as they can focus on just the problem part.

This code doesn't even work for me (except the first time because it skips the offending code the first time).
Like Leo says, the problem is around your Right(...-46) part of code:

Error at line 25, position 6
 Invalid procedure call or argument: 'Right' (0x800a0005)

Many thanks Leo and bytespiller. I have found the problem and you were both right about it being around the area (Right (...46).

I have completely revised the logic around this area and the macro works a treat.

Never once got bytespiller's error, however.

Once again many thanks for your time.