Tak vyriešené. ChatGPT to dal na prvý pokus.
1. knižnica: C:\Program Files\Altap Salamander\plugins\automation\scripts-libs\RenamingTemplate.vbs (môžem volať z ďalších XYZ skriptov)
Code: Select all
Sub RenameFilesWithTemplate(Items, Template)
Dim Item, Fso, OriginalName, NameOnly, Ext, NewName
Set Fso = CreateObject("Scripting.FileSystemObject")
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 = ApplyTemplate(Template, NameOnly, Ext)
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 Sub
Function ApplyTemplate(Template, NameOnly, Ext)
Dim NewName, re, matches, match, startPos, endPos, lengthVal, subText
NewName = Template
NewName = Replace(NewName, "$(ExtPart)", Ext)
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
ApplyTemplate = NewName
End Function
Function CombinePath(folder, file)
If Right(folder, 1) = "\" Then
CombinePath = folder & file
Else
CombinePath = folder & "\" & file
End If
End Function
Sub RunRenaming()
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
Call RenameFilesWithTemplate(Items, Template)
End If
End Sub
2. volanie: C:\Program Files\Altap Salamander\plugins\automation\scripts\RenameWithTemplate.vbs
Code: Select all
Call IncludeFile("C:\Program Files\Altap Salamander\plugins\automation\scripts-libs\RenamingTemplate.vbs")
Template = "Show Name $(OriginalName:16,18) ..year ..resolution.$(ExtPart)"
Call RunRenaming()
Sub IncludeFile(filePath)
Dim fso, file, code
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile(filePath, 1)
code = file.ReadAll
file.Close
ExecuteGlobal code
End Sub
Hodnota pre 'Template' je rovnaká ako v dialógu pre premenovanie (ak by som išiel tou cestou). To bolo cieľom.