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.