Advertisement
Guest User

Rename Show - template.vbs

a guest
May 29th, 2025
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Dim Items
  2. If Salamander.SourcePanel.SelectedItems.Count = 0 Then
  3.     If Salamander.MsgBox("No items are selected. Rename all items in panel?", 4, "Question") = 6 Then
  4.         Set Items = Salamander.SourcePanel.Items
  5.     End If
  6. Else
  7.     Set Items = Salamander.SourcePanel.SelectedItems
  8. End If
  9.  
  10. If VarType(Items) <> 0 Then
  11.     Dim Item, Fso, OriginalName, NameOnly, Ext, Template, NewName
  12.     Set Fso = CreateObject("Scripting.FileSystemObject")
  13.  
  14.     Template = "Show Name $(OriginalName:16,18) ..year ..resolution.$(ExtPart)"
  15.  
  16.     For Each Item In Items
  17.         If Item.Name <> ".." And InStr(Item.Name, ".") > 0 Then
  18.             OriginalName = Item.Name
  19.             Ext = Fso.GetExtensionName(OriginalName)
  20.             NameOnly = Left(OriginalName, Len(OriginalName) - Len(Ext) - 1)
  21.  
  22.             NewName = Template
  23.             NewName = Replace(NewName, "$(ExtPart)", Ext)
  24.  
  25.             Dim re, matches, match, startPos, endPos, lengthVal, subText
  26.             Set re = New RegExp
  27.             re.Pattern = "\$\(\s*OriginalName\s*:(\d+)\s*,\s*(\d+)\s*\)"
  28.             re.Global = True
  29.  
  30.             Set matches = re.Execute(NewName)
  31.  
  32.             For Each match In matches
  33.                 startPos = CInt(match.SubMatches(0))
  34.                 endPos = CInt(match.SubMatches(1))
  35.  
  36.                 If startPos > 0 And endPos >= startPos And endPos <= Len(NameOnly) Then
  37.                     lengthVal = endPos - startPos + 1
  38.                     subText = Mid(NameOnly, startPos, lengthVal)
  39.                 Else
  40.                     subText = ""
  41.                 End If
  42.  
  43.                 NewName = Replace(NewName, match.Value, subText)
  44.             Next
  45.  
  46.             Dim OldFullPath, NewFullPath, ParentFolder
  47.             OldFullPath = Item.Path
  48.             ParentFolder = Fso.GetParentFolderName(Item.Path)
  49.             NewFullPath = CombinePath(ParentFolder, NewName)
  50.  
  51.             If Len(NewName) = 0 Then
  52.                 ' skip empty name
  53.            ElseIf NewName = OriginalName Then
  54.                 ' skip unchanged name
  55.            ElseIf Fso.FileExists(NewFullPath) Then
  56.                 ' skip if target file exists
  57.            Else
  58.                 On Error Resume Next
  59.                 Fso.MoveFile OldFullPath, NewFullPath
  60.                 On Error GoTo 0
  61.             End If
  62.         End If
  63.     Next
  64.  
  65.     Salamander.MsgBox "Renaming finished.", 64, "Done"
  66. End If
  67.  
  68. Function CombinePath(folder, file)
  69.     If Right(folder, 1) = "\" Then
  70.         CombinePath = folder & file
  71.     Else
  72.         CombinePath = folder & "\" & file
  73.     End If
  74. End Function
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement