这个脚本自动给这些文件夹加上媒体文件统计信息后缀(常用于整理写真、漫画、视频集等资源)
具体逻辑如下:
- 自动获取路径:不需要写死盘符,它会自动作用于当前 .vbs 脚本所在的文件夹下的所有一级子目录。
- 定义媒体后缀:预设了常见的图片扩展名(jpg, png, webp, heic 等)和视频扩展名(mp4, mkv, mov, ts 等)。
- 递归统计数量与大小:
- 统计该文件夹(及更深层子目录)内共有多少张图片(P)和多少个视频(V)。
- 汇总所有媒体文件的字节数大小,并格式化为 MB 或 GB(例如:500MB 或 1.5GB)。
- 格式化重命名:
- 如果包含视频,生成后缀格式: [图片数P视频数V-总大小](例如: [100P2V-1.5GB])
- 如果只有图片,生成后缀格式: [图片数P-总大小](例如: [100P-500MB])
- 智能覆盖更新:使用了正则表达式 RemoveOldSuffix,如果文件夹之前已经被这个脚本重命名过,再次运行它会先擦除旧后缀再重新计算,不会造成后缀重复叠加。
保存脚本:后缀为 .vbs
Option Explicit
Dim fso, rootPath, rootFolder
Dim imageExts, videoExts
Dim folder, folders
Dim renamedCount, skipCount
Dim Recursive
Recursive = True
Set fso = CreateObject("Scripting.FileSystemObject")
rootPath = fso.GetParentFolderName(WScript.ScriptFullName)
Set rootFolder = fso.GetFolder(rootPath)
Set imageExts = CreateObject("Scripting.Dictionary")
Set videoExts = CreateObject("Scripting.Dictionary")
Set folders = CreateObject("Scripting.Dictionary")
AddExt imageExts, "jpg,jpeg,png,gif,webp,bmp,tif,tiff,heic,heif,avif"
AddExt videoExts, "mp4,mkv,avi,mov,wmv,flv,webm,m4v,ts,mts,m2ts,rm,rmvb,3gp"
For Each folder In rootFolder.SubFolders
folders.Add folder.Path, folder.Name
Next
renamedCount = 0
skipCount = 0
Dim folderPath
For Each folderPath In folders.Keys
ProcessFolder folderPath
Next
MsgBox "Done!" & vbCrLf & _
"Renamed: " & renamedCount & vbCrLf & _
"Skipped: " & skipCount, vbInformation, "Finished"
Sub AddExt(dict, extList)
Dim arr, i, ext
arr = Split(extList, ",")
For i = 0 To UBound(arr)
ext = LCase(Trim(arr(i)))
If Not dict.Exists(ext) Then
dict.Add ext, True
End If
Next
End Sub
Sub ProcessFolder(folderPath)
On Error Resume Next
Dim targetFolder
Set targetFolder = fso.GetFolder(folderPath)
If Err.Number <> 0 Then
Err.Clear
skipCount = skipCount + 1
Exit Sub
End If
Dim imgCount, vidCount, totalBytes
imgCount = 0
vidCount = 0
totalBytes = 0
CountMedia targetFolder, imgCount, vidCount, totalBytes
Dim sizeText, suffix, baseName, newName, newPath
sizeText = FormatSize(totalBytes)
If vidCount > 0 Then
suffix = "[" & imgCount & "P" & vidCount & "V-" & sizeText & "]"
Else
suffix = "[" & imgCount & "P-" & sizeText & "]"
End If
baseName = RemoveOldSuffix(targetFolder.Name)
newName = baseName & " " & suffix
newPath = fso.BuildPath(targetFolder.ParentFolder.Path, newName)
If targetFolder.Name = newName Then
skipCount = skipCount + 1
Exit Sub
End If
If fso.FolderExists(newPath) Then
skipCount = skipCount + 1
Exit Sub
End If
Err.Clear
targetFolder.Name = newName
If Err.Number <> 0 Then
Err.Clear
skipCount = skipCount + 1
Else
renamedCount = renamedCount + 1
End If
On Error GoTo 0
End Sub
Sub CountMedia(folderObj, ByRef imgCount, ByRef vidCount, ByRef totalBytes)
On Error Resume Next
Dim file, subFolder, ext
For Each file In folderObj.Files
ext = LCase(fso.GetExtensionName(file.Name))
If imageExts.Exists(ext) Then
imgCount = imgCount + 1
totalBytes = totalBytes + CDbl(file.Size)
ElseIf videoExts.Exists(ext) Then
vidCount = vidCount + 1
totalBytes = totalBytes + CDbl(file.Size)
End If
Next
If Recursive = True Then
For Each subFolder In folderObj.SubFolders
CountMedia subFolder, imgCount, vidCount, totalBytes
Next
End If
On Error GoTo 0
End Sub
Function RemoveOldSuffix(nameText)
Dim reg
Set reg = CreateObject("VBScript.RegExp")
reg.Pattern = "\s*\[\d+P(\d+V)?-[^\]]+\]$"
reg.IgnoreCase = True
reg.Global = False
RemoveOldSuffix = reg.Replace(nameText, "")
End Function
Function FormatSize(bytes)
Dim mb, gb, txt
mb = bytes / 1024 / 1024
If mb >= 1024 Then
gb = bytes / 1024 / 1024 / 1024
txt = CStr(Round(gb, 2))
txt = Replace(txt, ",", ".")
If InStr(txt, ".") > 0 Then
Do While Right(txt, 1) = "0"
txt = Left(txt, Len(txt) - 1)
Loop
If Right(txt, 1) = "." Then
txt = Left(txt, Len(txt) - 1)
End If
End If
FormatSize = txt & "GB"
Else
FormatSize = CStr(CLng(mb + 0.5)) & "MB"
End If
End Function
评论 0
欢迎参与讨论,请保持友善与尊重。