Generating the hash value of a file

后端 未结 2 1252
粉色の甜心
粉色の甜心 2021-01-07 06:46

I have managed to gather code and tried to generate the hash value of a file, but in the present code I need to drag the file on the VBScript, then it gives the hash value.<

2条回答
  •  情深已故
    2021-01-07 07:37

    Here we go:

    Set fso = CreateObject("Scripting.FileSystemObject")
    Dim oMD5:  Set oMD5 = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
    Dim oLog 'As Scripting.TextStream
    
    Set oArgs = WScript.Arguments
    
    If oArgs.Count = 1 Then
        sFolderPath = GetFolderPath()
        Set oLog = fso.CreateTextFile(sFolderPath & "\FileHash.csv", True)
        oLog.Write "sep=" & vbTab & vbCrLf
        CheckFolder oArgs(I)
        oLog.Close
        Msgbox "Done!"
    Else
        Msgbox "Drop Folder"
    End If
    
    Sub CheckFolder(sFolderPath)
        Dim sKey
        Dim oFolder 'As Scripting.Folder
        Set oFolder = fso.GetFolder(sFolderPath)
    
        For Each oFile In oFolder.Files
            oLog.Write oFile.Path & vbTab & GetMd5(oFile.Path) & vbCrLf
        Next
    
        For Each oChildFolder In oFolder.SubFolders
            CheckFolder oChildFolder.Path
        Next
    End Sub
    
    Function GetFolderPath()
        Dim oFile 'As Scripting.File
        Set oFile = fso.GetFile(WScript.ScriptFullName)
        GetFolderPath = oFile.ParentFolder
    End Function
    
    Function GetMd5(filename)
        Dim oXml, oElement
    
        oMD5.ComputeHash_2(GetBinaryFile(filename))
    
        Set oXml = CreateObject("MSXML2.DOMDocument")
        Set oElement = oXml.CreateElement("tmp")
        oElement.DataType = "bin.hex"
        oElement.NodeTypedValue = oMD5.Hash
        GetMd5 = oElement.Text
    End Function
    
    Function GetBinaryFile(filename)
        Dim oStream: Set oStream = CreateObject("ADODB.Stream")
        oStream.Type = 1 'adTypeBinary
        oStream.Open
        oStream.LoadFromFile filename
        GetBinaryFile= oStream.Read
        oStream.Close
        Set oStream = Nothing
    End Function
    

提交回复
热议问题