UMU WSH Git:http://git.oschina.net/umu618/umu-wsh/ git
本篇爲 UMU WSH 教程終結篇,之後更傾向使用 Windows PowerShell 和 golang 來編寫工具。 golang
早期 WP 拍攝的視頻,命名格式爲 WP_YYYYMMDD_xxx.mp4,丟失了拍攝的時間,後來的版本是 WP_YYYYMMDD_HH_mm_SS_Pro.mp4,這纔是滿意的格式,因此特意寫了這個腳本,將早期的 WP 自動按 LastModified 時間命名爲 YYYY-MM-DD_HH-mm-SS.mp4 的格式。 ide
直接看代碼吧! 工具
' 48_RenameWPVideoByLastModifiedTime.VBS ' UMU @ 15:40 2016/01/20 ' [UMU WSH 教程](48) FSO 應用實例 - 按文件最後修改時間批量重命名 WP 視頻文件 Option Explicit Const APP_TITLE = "UMU.Script.Tools.RenameWPVideoByLastModifiedTime" Const APP_DESCRIPTION = "本程序用來把 WP 視頻文件按最後修改時間批量重命名。" Const APP_USAGE = "請把要處理的文件或文件夾拖放到本程序的圖標上!" Const OLD_TIME = #1900/01/01 00:00:00# Dim args, fso, wi Set args = WScript.Arguments Set fso = CreateObject("Scripting.FileSystemObject") Set wi = CreateObject("WindowsInstaller.Installer") If args.Count = 0 Then Usage() Else Dim is_move Dim is_logging Dim log_file Dim succeeded_count, failed_count, exists_count Main() End If Set args = Nothing Set fso = Nothing Set wi = Nothing Private Sub Usage() Dim wsh Dim send_to, copy_to MsgBox APP_DESCRIPTION & vbCrLf & APP_USAGE, vbInformation, APP_TITLE Set wsh = CreateObject("WScript.Shell") send_to = wsh.SpecialFolders("SendTo") copy_to = send_to & "\" & APP_TITLE & ".VBE" Dim copy_to_sendto If Not fso.FileExists(copy_to) Then copy_to_sendto = True ElseIf Not IsFileTheSame(copy_to, WScript.ScriptFullName) Then copy_to_sendto = True Else copy_to_sendto = False End If If copy_to_sendto Then If vbOK = MsgBox(APP_DESCRIPTION & vbCrLf & APP_USAGE & vbCrLf & vbCrLf & _ "提示:您能夠把此文件放在 Sendto 目錄裏,而後使用右鍵菜單的「發送到」。" & vbCrLf & _ "您的 Sendto 目錄是 " & send_to & vbCrLf & "按「肯定」執行復制操做。", _ vbOKCancel + vbInformation, APP_TITLE) Then fso.CopyFile WScript.ScriptFullName, copy_to If vbYes = MsgBox("是否查看 Sendto 目錄?", vbQuestion + vbYesNo, APP_TITLE) Then wsh.Run "%SystemRoot%\explorer.exe /n, /select," & copy_to End If End If End If Set wsh = Nothing End Sub Private Sub Main() is_move = MsgBox("重命名文件?按「否」複製文件,按「取消」退出!", vbYesNoCancel + vbQuestion, "詢問") If vbCancel = is_move Then Exit Sub End If is_logging = MsgBox("產生日誌?按「取消」退出!", vbYesNoCancel + vbQuestion, "詢問") If vbCancel = is_logging Then Exit Sub End If If is_logging = vbYes Then Set log_file = fso.CreateTextFile(fso.GetSpecialFolder(2) & "\" & APP_TITLE & ".log") End If succeeded_count = 0 failed_count = 0 exists_count = 0 Dim ar For Each ar In args If fso.FolderExists(ar) Then Call RenameWPVideoByLastModifiedTime_s(ar) ElseIf fso.FileExists(ar) Then Call RenameWPVideoByLastModifiedTime(ar) End If Next If is_logging = vbYes Then log_file.Close Set log_file = Nothing End If MsgBox "重命名 " & succeeded_count & " 個,失敗 " & failed_count & _ " 個,文件已經存在 " & exists_count & " 個!", 4160, "整個世界清淨了!" End Sub Private Sub RenameWPVideoByLastModifiedTime_s(ByVal folder_path) 'On Error Resume Next Dim rfd, fs, f, fds, fd Set rfd = fso.GetFolder(folder_path) Set fs = rfd.Files For Each f In fs Call RenameWPVideoByLastModifiedTime(f.Path) Next Set fds = rfd.SubFolders For Each fd In fds Call RenameWPVideoByLastModifiedTime_s(fd.Path) Next End Sub Private Sub RenameWPVideoByLastModifiedTime(ByRef file_path) 'On Error Resume Next Dim path_pos Dim file_name path_pos = InStrRev(file_path, "\") file_name = Mid(file_path, path_pos + 1) If 0 <> StrComp(Left(file_name, 3), "WP_", vbBinaryCompare) Then ' 不是 WP_ 開頭,跳過 Exit Sub End If If 0 <> StrComp(Right(file_name, 4), ".mp4", vbTextCompare) Then ' 不是 .MP4,跳過 Exit Sub End If If 19 <> Len(file_name) Then ' WP 視頻命名有兩種:WP_YYYYMMDD_HH_mm_SS_Pro.mp4(28) ' WP_YYYYMMDD_xxx.mp4(19),前者不須要處理 ' 文件名太長,跳過 Exit Sub End If Dim date_part date_part = Mid(file_name, 4, 8) If Not IsNumeric(date_part) Then ' 不是日期格式的數字 Exit Sub End If Dim date_time date_time = GetFileModifiedTime(file_path) If date_time > OLD_TIME Then Dim dt dt = MyFormatDateTime(date_time) If 0 <> StrComp(date_part, MyFormatDate(date_time)) Then If is_logging = vbYes Then log_file.WriteLine file_path log_file.WriteLine "#" & dt log_file.WriteLine "----------------" End If Exit Sub End If Dim path path = Left(file_path, path_pos) Dim ext ext = Mid(file_path, InStrRev(file_path, ".")) path = path & dt & ext If fso.FileExists(path) Then exists_count = exists_count + 1 If IsFileTheSame(file_path, path) Then fso.DeleteFile file_path If is_logging = vbYes Then log_file.WriteLine "~" & file_path log_file.WriteLine "@" & path log_file.WriteLine "----------------" End If Else If is_logging = vbYes Then log_file.WriteLine file_path log_file.WriteLine "@" & path log_file.WriteLine "----------------" End If End If ElseIf vbYes = is_move Then fso.MoveFile file_path, path If Err.Number <> 0 Then failed_count = failed_count + 1 Err.Clear If is_logging = vbYes Then log_file.WriteLine "~" & file_path log_file.WriteLine "-" & path log_file.WriteLine "----------------" End If Else succeeded_count = succeeded_count + 1 If is_logging = vbYes Then log_file.WriteLine "~" & file_path log_file.WriteLine "+" & path log_file.WriteLine "----------------" End If End If Else fso.CopyFile file_path, path If Err.Number <> 0 Then failed_count = failed_count + 1 Err.Clear If is_logging = vbYes Then log_file.WriteLine "&" & file_path log_file.WriteLine "-" & path log_file.WriteLine "----------------" End If Else succeeded_count = succeeded_count + 1 If is_logging = vbYes Then log_file.WriteLine "&" & file_path log_file.WriteLine "+" & path log_file.WriteLine "----------------" End If End If End If Else ' 沒有最後修改時間 If is_logging = vbYes Then log_file.WriteLine file_path log_file.WriteLine "!" log_file.WriteLine "----------------" End If End If End Sub Private Function TimeValue(num) TimeValue = Right("0" & num, 2) End Function Private Function MyFormatDateTime(ByRef dt) MyFormatDateTime = Year(dt) & "-" & TimeValue(Month(dt)) & "-" & TimeValue(Day(dt)) & "_" & TimeValue(Hour(dt)) & "-" & TimeValue(Minute(dt)) & "-" & TimeValue(Second(dt)) End Function Private Function MyFormatDate(ByRef dt) MyFormatDate = Year(dt) & TimeValue(Month(dt)) & TimeValue(Day(dt)) End Function Private Function GetFileModifiedTime(ByRef file_path) On Error Resume Next GetFileModifiedTime = OLD_TIME Dim file Set file = fso.GetFile(file_path) GetFileModifiedTime = file.DateLastModified Set file = Nothing End Function Private Function BigEndianHex(int) Dim result Dim b1, b2, b3, b4 result = Right("0000000" & Hex(int), 8) b1 = Mid(result, 7, 2) b2 = Mid(result, 5, 2) b3 = Mid(result, 3, 2) b4 = Mid(result, 1, 2) BigEndianHex = b1 & b2 & b3 & b4 End Function Private Function GetFileHash(file_name) Dim file_hash Dim hash_value Dim i Set file_hash = wi.FileHash(file_name, 0) hash_value = "" For i = 1 To file_hash.FieldCount hash_value = hash_value & BigEndianHex(file_hash.IntegerData(i)) Next Set file_hash = Nothing GetFileHash = hash_value End Function Private Function IsFileTheSame(ByRef file1, ByRef file2) If 0 = StrComp(file1, file2, vbTextCompare) Then IsFileTheSame = True Else Dim hash1, hash2 hash1 = GetFileHash(file1) hash2 = GetFileHash(file2) If hash1 = hash2 And Len(hash1) > 0 Then IsFileTheSame = True Else IsFileTheSame = False End If End If End Function