Here's a video showing what the button does:
Select the last folder (the one you want to duplicate), and then click the button. It will ask you how many copies you want made and then make them.
Duplicate folders are named using the incremented number of the selected folder (and then one more each time), plus whatever is in the middle of the selected folder name, plus today's date in MMDDYY format at the end.
The script will check that there aren't already any folders with a higher number than the selected folder, and warn you if there are. (You can choose whether to continue or not when that happens.)
Once all the duplicates are created, they will be selected, so you can see what was done.
--
Here's the script itself, just for reference. Most of the logic is error checking to try to avoid making a mess if something unexpected is selected.
[code]option explicit
Function ZeroPad(str, slen)
str = CStr(Str) ' Force To String.
If (Len(str) < slen) Then
str = String(slen - Len(str), "0") & str
End If
ZeroPad = str
End Function
Function IsLongInteger(str)
IsLongInteger = False
If (IsNumeric(str)) Then
' Not really needed, but make sure the number doesn't have a decimal point.
If (CStr(CLng(str)) = CStr(str)) Then
IsLongInteger = True
End If
End If
End Function
Function OnClick(ClickData)
Dim dlgTitle, tab, dlg, cmd, numFolders, regexp, matches
Dim fullName, namePrefix, nameSuffix, prefixLen, nowDate, i
Dim fsu, folderEnum, currentPrefix
dlgTitle = "Duplicate template folder"
nowDate = Date()
Set tab = Clickdata.func.sourcetab
Set dlg = ClickData.func.Dlg
Set cmd = ClickData.func.command
Set regexp = New RegExp
Set fsu = DOpus.FSUtil
cmd.deselect = False
' Check exactly one folder is selected.
If (tab.selected_dirs.count <> 1 Or tab.selected_files.count <> 0) Then
dlg.Request "Select one folder to duplicate.", "OK", dlgTitle
Exit Function
End If
' Split the folder name up and put today's date on the end.
regexp.IgnoreCase = True
regexp.Global = True
regexp.Pattern = "^(\d+)(_.+_)\d{6}$"
Set matches = regexp.Execute(tab.selected_dirs(0).name)
If (matches.count < 1) Then
dlg.Request "Selected folder's name does not fit the expected pattern.", "OK", dlgTitle
Exit Function
End If
namePrefix = matches(0).SubMatches(0)
nameSuffix = matches(0).SubMatches(1)
prefixLen = Len(namePrefix)
namePrefix = CLng(namePrefix)
nameSuffix = nameSuffix & ZeroPad(Month(nowDate),2) & ZeroPad(Day(nowDate),2) & ZeroPad(Right(Year(nowDate),2),2)
' Ask how many copies to make.
dlg.Select = True
dlg.default = "1" ' Default number of folders to create
dlg.max = 4 ' Max string length. More than 9999 folders would probably be a mistake.
dlg.buttons = "OK|Cancel"
dlg.message = "How many new folders are needed?"
dlg.title = dlgTitle
If (dlg.Show() <> 1) Then
Exit Function ' Cancelled
End If
numFolders = dlg.Input
If (Not IsLongInteger(numFolders)) Then
dlg.Request "Invalid number of folders entered.", "OK", dlgTitle
Exit Function
End If
numFolders = CLng(numFolders)
' Warn if any prefix numbers in the range we are about to create are already in use.
Set folderEnum = fsu.ReadDir(Clickdata.func.sourcetab.path, False)
Do While Not folderEnum.Complete
Set matches = regexp.Execute(folderEnum.Next.name)
If (matches.count > 0) Then
currentPrefix = CLng(matches(0).SubMatches(0))
If (currentPrefix >= (namePrefix + 1)) Then
If (dlg.Request("Folders already exist with prefix " & (namePrefix + 1) & " or above." & vbCrLf & "Are you sure you wish to continue?", "Continue|Cancel", dlgTitle) <> 1) Then
Exit Function
End If
Exit Do
End If
End If
Loop
' Create the duplicate folders
currentPrefix = namePrefix
For i = 1 To numFolders
currentPrefix = currentPrefix + 1
fullName = ZeroPad(currentPrefix, prefixLen) & nameSuffix
cmd.RunCommand "Copy DUPLICATE HERE AS=""" & fullName & """"
Next
' Select what was created
cmd.RunCommand "Select NONE"
currentPrefix = namePrefix
For i = 1 To numFolders
currentPrefix = currentPrefix + 1
fullName = ZeroPad(currentPrefix, prefixLen) & nameSuffix
cmd.RunCommand "Select MAKEVISIBLE EXACT PATTERN=""" & fullName & """"
Next
End Function[/code]
Here's the button in XML format, ready to paste to your toolbar.
See How to add buttons from this forum to your toolbars, specifically the second part that talks about XML button definitions.
<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none">
<label>Duplicate Template Folder</label>
<icon1>#default:newcollection</icon1>
<function type="script">
<instruction>option explicit</instruction>
<instruction />
<instruction>Function ZeroPad(str, slen)</instruction>
<instruction> str = CStr(Str) ' Force To String.</instruction>
<instruction> If (Len(str) < slen) Then</instruction>
<instruction> str = String(slen - Len(str), "0") & str</instruction>
<instruction> End If</instruction>
<instruction> ZeroPad = str</instruction>
<instruction>End Function</instruction>
<instruction />
<instruction>Function IsLongInteger(str)</instruction>
<instruction> IsLongInteger = False</instruction>
<instruction> If (IsNumeric(str)) Then</instruction>
<instruction> ' Not really needed, but make sure the number doesn't have a decimal point.</instruction>
<instruction> If (CStr(CLng(str)) = CStr(str)) Then</instruction>
<instruction> IsLongInteger = True</instruction>
<instruction> End If</instruction>
<instruction> End If</instruction>
<instruction>End Function</instruction>
<instruction />
<instruction>Function OnClick(ClickData)</instruction>
<instruction> Dim dlgTitle, tab, dlg, cmd, numFolders, regexp, matches</instruction>
<instruction> Dim fullName, namePrefix, nameSuffix, prefixLen, nowDate, i</instruction>
<instruction> Dim fsu, folderEnum, currentPrefix</instruction>
<instruction> dlgTitle = "Duplicate template folder"</instruction>
<instruction> nowDate = Date()</instruction>
<instruction> Set tab = Clickdata.func.sourcetab</instruction>
<instruction> Set dlg = ClickData.func.Dlg</instruction>
<instruction> Set cmd = ClickData.func.command</instruction>
<instruction> Set regexp = New RegExp</instruction>
<instruction> Set fsu = DOpus.FSUtil</instruction>
<instruction />
<instruction> cmd.deselect = False</instruction>
<instruction />
<instruction> ' Check exactly one folder is selected.</instruction>
<instruction />
<instruction> If (tab.selected_dirs.count <> 1 Or tab.selected_files.count <> 0) Then</instruction>
<instruction> dlg.Request "Select one folder to duplicate.", "OK", dlgTitle</instruction>
<instruction> Exit Function</instruction>
<instruction> End If</instruction>
<instruction />
<instruction> ' Split the folder name up and put today's date on the end.</instruction>
<instruction />
<instruction> regexp.IgnoreCase = True</instruction>
<instruction> regexp.Global = True</instruction>
<instruction> regexp.Pattern = "^(\d+)(_.+_)\d{6}$"</instruction>
<instruction> Set matches = regexp.Execute(tab.selected_dirs(0).name)</instruction>
<instruction> If (matches.count < 1) Then</instruction>
<instruction> dlg.Request "Selected folder's name does not fit the expected pattern.", "OK", dlgTitle</instruction>
<instruction> Exit Function</instruction>
<instruction> End If</instruction>
<instruction />
<instruction> namePrefix = matches(0).SubMatches(0)</instruction>
<instruction> nameSuffix = matches(0).SubMatches(1)</instruction>
<instruction />
<instruction> prefixLen = Len(namePrefix)</instruction>
<instruction> namePrefix = CLng(namePrefix)</instruction>
<instruction />
<instruction> nameSuffix = nameSuffix & ZeroPad(Month(nowDate),2) & ZeroPad(Day(nowDate),2) & ZeroPad(Right(Year(nowDate),2),2)</instruction>
<instruction />
<instruction> ' Ask how many copies to make.</instruction>
<instruction />
<instruction> dlg.Select = True</instruction>
<instruction> dlg.default = "1" ' Default number of folders to create</instruction>
<instruction> dlg.max = 4 ' Max string length. More than 9999 folders would probably be a mistake.</instruction>
<instruction> dlg.buttons = "OK|Cancel"</instruction>
<instruction> dlg.message = "How many new folders are needed?"</instruction>
<instruction> dlg.title = dlgTitle</instruction>
<instruction> If (dlg.Show() <> 1) Then</instruction>
<instruction> Exit Function ' Cancelled</instruction>
<instruction> End If</instruction>
<instruction> numFolders = dlg.Input</instruction>
<instruction />
<instruction> If (Not IsLongInteger(numFolders)) Then</instruction>
<instruction> dlg.Request "Invalid number of folders entered.", "OK", dlgTitle</instruction>
<instruction> Exit Function</instruction>
<instruction> End If</instruction>
<instruction />
<instruction> numFolders = CLng(numFolders)</instruction>
<instruction />
<instruction> ' Warn if any prefix numbers in the range we are about to create are already in use.</instruction>
<instruction> </instruction>
<instruction> Set folderEnum = fsu.ReadDir(Clickdata.func.sourcetab.path, False)</instruction>
<instruction> Do While Not folderEnum.Complete</instruction>
<instruction> Set matches = regexp.Execute(folderEnum.Next.name)</instruction>
<instruction> If (matches.count > 0) Then</instruction>
<instruction> currentPrefix = CLng(matches(0).SubMatches(0))</instruction>
<instruction> If (currentPrefix >= (namePrefix + 1)) Then</instruction>
<instruction> If (dlg.Request("Folders already exist with prefix " & (namePrefix + 1) & " or above." & vbCrLf & "Are you sure you wish to continue?", "Continue|Cancel", dlgTitle) <> 1) Then</instruction>
<instruction> Exit Function</instruction>
<instruction> End If</instruction>
<instruction> Exit Do</instruction>
<instruction> End If</instruction>
<instruction> End If</instruction>
<instruction> Loop</instruction>
<instruction />
<instruction> ' Create the duplicate folders</instruction>
<instruction />
<instruction> currentPrefix = namePrefix</instruction>
<instruction />
<instruction> For i = 1 To numFolders</instruction>
<instruction> currentPrefix = currentPrefix + 1</instruction>
<instruction> fullName = ZeroPad(currentPrefix, prefixLen) & nameSuffix</instruction>
<instruction> cmd.RunCommand "Copy DUPLICATE HERE AS=""" & fullName & """"</instruction>
<instruction> Next</instruction>
<instruction />
<instruction> ' Select what was created</instruction>
<instruction />
<instruction> cmd.RunCommand "Select NONE"</instruction>
<instruction />
<instruction> currentPrefix = namePrefix</instruction>
<instruction />
<instruction> For i = 1 To numFolders</instruction>
<instruction> currentPrefix = currentPrefix + 1</instruction>
<instruction> fullName = ZeroPad(currentPrefix, prefixLen) & nameSuffix</instruction>
<instruction> cmd.RunCommand "Select MAKEVISIBLE EXACT PATTERN=""" & fullName & """"</instruction>
<instruction> Next</instruction>
<instruction />
<instruction>End Function</instruction>
</function>
</button>