為了快速校驗備份檔案完整性,將「計算 MD5」加入檔案右鍵選單。使用 CAPICOM 計算雜湊,結果自動寫入剪貼簿 (需 Word 應用程式輔助),適用於 Windows 7/10/11。
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell\HASH(&G)\command]
@="WScript.exe //nologo C:\Windows\System32\GetMD5.vbs \"%1\""
&G 設定熱鍵 Alt+G (G 字母有底線)。C:\Windows\System32\Public MD5Value
MD5Sum WScript.Arguments(0)
SetClipboardText "MD5: " & MD5Value & ", FilePath: " & WScript.Arguments(0)
Function MD5Sum(filename)
Dim stream, hashed
Set stream = CreateObject("ADODB.Stream")
stream.Type = 1 ' adTypeBinary
stream.Open
stream.LoadFromFile filename
Set hashed = CreateObject("CAPICOM.HashedData")
hashed.Algorithm = 3 ' CAPICOM_HASH_ALGORITHM_MD5
hashed.Hash stream.Read
stream.Close
MD5Value = hashed.Value
End Function
Sub SetClipboardText(text)
Dim word
Set word = CreateObject("Word.Application")
word.Documents.Add
word.Selection.Text = text
word.Selection.Copy
word.Quit False
End Sub
capicom.dll 需註冊 (regsvr32 capicom.dll),Windows 10/11 可能需手動安裝 SDK 或從舊系統複製註冊。Sub SetClipboardText(text)
' 使用 DataObject (需參考 "Microsoft Forms 2.0 Object Library" 或晚綁定)
Dim obj
Set obj = CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}") ' DataObject
obj.SetText text
obj.PutInClipboard
End Sub
或使用 PowerShell 單列指令:
powershell -sta -c "Set-Clipboard -Value 'MD5: xxx'"
ADODB.Stream 讀取大檔案較 FileSystemObject 快。hashed.Algorithm = 4)。MD5: d41d8cd98f00b204e9800998ecf8427e, FilePath: C:\test.txt。此小工具極大提升檔案校驗效率,適合系統管理、備份驗證、軟體分發等場景。