Best Way to Handle a "Cancel" from VBScript InputBox

Does anyone have a recommendation for the best way to abort the Rename task when using a VBScript InputBox and the user presses the "Cancel" button?

The VBScript InputBox just returns an empty string if the user presses the "Cancel" button. It's easy enough to test for a string with zero length, but is there a good way to abort the DOpus Rename process?

If the InputBox string is zero length I can set strNewName = strFileName, but it seems like it would be better to just to Exit out of the function and abort the whole Rename task. (VBScript 'Exit' doesn't seem to work, probably due to using a var by reference)

For Example:
(The actual rename isn't important, I'm just trying to illustrate the "abort this rename" concept)

@script vbscript
Option Explicit

Dim baseName

Function Rename_GetNewName(strFileName, strFilePath, fIsFolder, strOldName, ByRef strNewName)

	If baseName <> "?Cancelled?" Then
	
		If Len(baseName) = 0 Then
	   		baseName = InputBox("Enter the base name:")
		End If
	
		If Len(baseName) <> 0 Then
			strNewName = baseName & " " & strNewName
		Else
			baseName = "?Cancelled?"
			strNewName = strFileName
			DOpus.OutputString "baseName Length = 0 - Aborted Rename"
		End If
	
		DOpus.OutputString "-----------------------------------------------------------"
		DOpus.OutputString "baseName = " & baseName
		DOpus.OutputString "strFileName = " & strFileName
		DOpus.OutputString "strFilePath = " & strFilePath
		DOpus.OutputString "fIsFolder = " & fIsFolder
		DOpus.OutputString "strOldName = " & strOldName
		DOpus.OutputString "strNewName = " & strNewName
		DOpus.OutputString "-----------------------------------------------------------"
	
	Else
		strNewName = strFileName
		DOpus.OutputString "baseName Length = 0 - Aborted Rename"
	End If

End Function

So if I try to rename 4 files and press "Cancel" in the InputBox I would get the following Output:

Script started successfully
baseName Length = 0 - Aborted Rename
-----------------------------------------------------------
baseName = ?Cancelled?
strFileName = Waterfall1.jpg
strFilePath = C:\Temp\Pictures\CostaRica
fIsFolder = False
strOldName = (.*)\.(.*)
strNewName = Waterfall1.jpg
-----------------------------------------------------------
baseName Length = 0 - Aborted Rename
baseName Length = 0 - Aborted Rename
baseName Length = 0 - Aborted Rename
Script completed

This works, but is there a better way to handle this situation? e.g. Not still process every file after the user presses cancel?

BTW - Thanks to everyone, especially nudel, for sharing their Rename scripts. It's very helpful. :slight_smile:

You should call Exit Function without changing strNewName if your script decides it shouldn't rename anything. It'll still be run for every file, though, but that should be harmless and shouldn't take long.

Of course, the files may still get renamed if the user is also doing a wildcard/regexp/etc. rename in addition to the script.

Thanks for the reply nudel.

You are correct. "Exit Function" works well.

But as you pointed out if the rename preset also specifies any other renaming, e.g. Auto Numbering, etc. you still have to set "strNewName = strFileName" to prevent the file from being renamed.

It's not a big deal... But it might be nice to have some sort of DOpus.Abort() method to just bail out of the Rename process without having to store the "Cancel" state and still loop through the remaining files.

Thanks again for all the great renaming script examples. :slight_smile: