option explicit ' GetHash ' qiuqiu (c) 2015 ' ' This is a script for Directory Opus. ' See http://www.gpsoft.com.au/DScripts/redirect.asp?page=scripts for development information. ' ' ' ' Called by Directory Opus to initialize the script Function OnInit(initData) initData.name = "GetHash" initData.desc = "Get Select Files Hash Code To Dialog,Clipboard,File." initData.copyright = "qiuqiu (c) 2015" initData.version = "1.0" initData.default_enable = True Dim cmd Set cmd = initData.AddCommand cmd.name = initData.name cmd.method = initData.name cmd.desc = initData.desc cmd.label = initData.name cmd.template = "MD5/S,SHA/S,MODE/K[Dialog,CLIPBOARD,FILE]" End Function ' Implement the GetHash command Function GetHash(scriptCmdData) Dim MsgDlg, File, FileCount, DirCount, ForCount, DlgMsg, HashProgress, HashFile FileCount = 0 : DirCount = 0 : ForCount = 0 Set HashProgress = scriptCmdData.Func.command.progress With HashProgress .abort = True .bytes = True .delay = False .full = True .owned = True .pause = False .skip = False .Init scriptCmdData.func.sourcetab.lister, "Hash Progress" .SetTitle "Calculate Hash" .InitFileSize .AddFiles scriptCmdData.Func.Command.Files.Count .Show End With For ForCount = 0 to scriptCmdData.Func.Command.Files.Count - 1 If HashProgress.GetAbortState = "a" Then Exit Function set File = scriptCmdData.Func.Command.Files(ForCount) HashProgress.SetName File.Name If (File.Is_dir) Then DirCount = DirCount + 1 HashProgress.SkipFile False Else DlgMsg = DlgMsg & vbCRLF & "File:" & vbTab & File.Name & vbCRLF If (scriptCmdData.func.args.got_arg.md5) And (HashProgress.GetAbortState <> "a") Then HashProgress.SetStatus "Calculating MD5, Please Waiting...." DlgMsg = DlgMsg & "MD5:" & vbTab & DOpus.FSUtil.Hash(File.realpath, "MD5") & vbCRLF End If If (scriptCmdData.func.args.got_arg.sha) And (HashProgress.GetAbortState <> "a") Then HashProgress.SetStatus "Calculating SHA-1, Please Waiting...." DlgMsg = DlgMsg & "SHA-1:" & vbTab & DOpus.FSUtil.Hash(File.realpath, "SHA") & vbCRLF End If FileCount = FileCount + 1 HashProgress.SkipFile True End If HashProgress.StepFiles 1 Next HashProgress.Hide DlgMsg = "Item Count: " & scriptCmdData.Func.Command.Files.Count & vbTab & vbTab & "Calculated: " & FileCount & vbTab & vbTab & "Skipped: " & DirCount & vbCRLF & String(37, "=") & Trim(DlgMsg) 'to clipboard If InStr(1, UCase(scriptCmdData.Func.args.Mode), UCase("Clipboard"), vbBinaryCompare) Then DOpus.SetClip DlgMsg End If 'to file If InStr(1, UCase(scriptCmdData.Func.args.Mode), UCase("File"), vbBinaryCompare) Then WriteToFile scriptCmdData.Func.Command.Source & "\Hash File.txt", DlgMsg, True End If 'to MsgDlg If InStr(1, UCase(scriptCmdData.Func.args.Mode), UCase("Dialog"), vbBinaryCompare) Then Set MsgDlg = scriptCmdData.Func.Dlg With MsgDlg .Window = scriptCmdData.func.sourcetab.lister .title = "Hash Completed" .buttons = "OK" .message = DlgMsg .Show End With End If Set File = Nothing Set DlgMsg = Nothing Set HashProgress = Nothing End Function Function BuildString (in_Str, length) buildString = in_Str & Space(length - Len(in_Str)) End Function Sub WriteToFile(FileName, Text, Mode) Const ForReading = 1, ForWriting = 2, ForAppending = 8 Dim fso, f, iomode Set fso = CreateObject("Scripting.FileSystemObject") if Mode = True then iomode = ForWriting Else iomode = ForAppending End If Set f = fso.OpenTextFile(FileName, iomode, True, True) f.Write Text f.Close Set f = Nothing Set fso = Nothing End Sub