Command: GetHash

This script add-in adds a GetHash command which gets the selected files' hashes and outputs them to a dialog, clipboard, or text file.

(Not to be confused with the built-in GetSizes command, which can calculate hashes and display them in file display columns.)

History:

  • 2016-07-04: Fixed incomplete display

Script File:

  • GetHash.vbs.txt (7.7 KB)
  • Download the file and drag it to Preferences / Toolbars / Scripts to install it.

Button Example:

To use this, you must have installed the script above.

See How to use buttons and scripts from this forum for how to paste the button code on to your toolbar.

<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" separate="yes" textcol="none">
	<label>Show &amp;MD5 and SHA</label>
	<tip>Show selected files' MD5 and SHA hashes</tip>
	<icon1>#misc</icon1>
	<function type="normal">
		<instruction>@disablenosel:files</instruction>
		<instruction />
		<instruction>GetHash MD5 SHA MODE=DIALOG </instruction>
	</function>
</button>

Script Code:

The script code from the download above is reproduced here for reference:

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

Nice Script!It's an efficient way for Checksum files。But this script can't display sha-1 hash correctly sometime.look my attach pic show

I've been fixing up old button/script posts from before we changed forum software, and noticed that this one could be sped up when calculating more than one hash type.

The DOpus.FSUtil.Hash method lets you specify multiple hash types so they can all be calculated at once. (We may have added this to Opus after this script was written. I'm not sure.) That can make it a lot faster with large files, since they only need to be read from disk once.

An example of how it works can be found in my post here in this thread Button: Show MD5/SHA (which is about some buttons that do similar things).