Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Dim Items
- If Salamander.SourcePanel.SelectedItems.Count = 0 Then
- If Salamander.MsgBox("No items are selected. Rename all items in panel?", 4, "Question") = 6 Then
- Set Items = Salamander.SourcePanel.Items
- End If
- Else
- Set Items = Salamander.SourcePanel.SelectedItems
- End If
- If VarType(Items) <> 0 Then
- Dim Item, Fso, OriginalName, NameOnly, Ext, Template, NewName
- Set Fso = CreateObject("Scripting.FileSystemObject")
- Template = "Show Name $(OriginalName:16,18) ..year ..resolution.$(ExtPart)"
- For Each Item In Items
- If Item.Name <> ".." And InStr(Item.Name, ".") > 0 Then
- OriginalName = Item.Name
- Ext = Fso.GetExtensionName(OriginalName)
- NameOnly = Left(OriginalName, Len(OriginalName) - Len(Ext) - 1)
- NewName = Template
- NewName = Replace(NewName, "$(ExtPart)", Ext)
- Dim re, matches, match, startPos, endPos, lengthVal, subText
- Set re = New RegExp
- re.Pattern = "\$\(\s*OriginalName\s*:(\d+)\s*,\s*(\d+)\s*\)"
- re.Global = True
- Set matches = re.Execute(NewName)
- For Each match In matches
- startPos = CInt(match.SubMatches(0))
- endPos = CInt(match.SubMatches(1))
- If startPos > 0 And endPos >= startPos And endPos <= Len(NameOnly) Then
- lengthVal = endPos - startPos + 1
- subText = Mid(NameOnly, startPos, lengthVal)
- Else
- subText = ""
- End If
- NewName = Replace(NewName, match.Value, subText)
- Next
- Dim OldFullPath, NewFullPath, ParentFolder
- OldFullPath = Item.Path
- ParentFolder = Fso.GetParentFolderName(Item.Path)
- NewFullPath = CombinePath(ParentFolder, NewName)
- If Len(NewName) = 0 Then
- ' skip empty name
- ElseIf NewName = OriginalName Then
- ' skip unchanged name
- ElseIf Fso.FileExists(NewFullPath) Then
- ' skip if target file exists
- Else
- On Error Resume Next
- Fso.MoveFile OldFullPath, NewFullPath
- On Error GoTo 0
- End If
- End If
- Next
- Salamander.MsgBox "Renaming finished.", 64, "Done"
- End If
- Function CombinePath(folder, file)
- If Right(folder, 1) = "\" Then
- CombinePath = folder & file
- Else
- CombinePath = folder & "\" & file
- End If
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement