file - Get FCIV (or same) checksum in VBA -
how can execute fciv , obtain hash file using vba?
every pure vba implementation have seen has been painfully slow (sometimes on minute per file). there may way tapping windows com library not aware of such method. (i hope someome knows of 1 though , you'll see why in second:)) best have been able come ugly workaound following suggestion may not suitable in scenarios there very fast command line utility available ms here: http://support.microsoft.com/kb/841290. utility md5 , sha1. although site says it's windows xp can verify works versions through , including windows 7. haven't tried on 64 bit though.
a few caveats:
1. utility unsupported. have never had issues it. it's still consideration.
2. utility have present on machine intended run code on , may not feasible in circumstances.
3. bit of hack/kludge may want test little error conditions etc.
4. banged together. haven't tested it/worked it. take 3 seriously:)
option explicit public enum ehashtype md5 sha1 end enum ''//update value wherever install fciv: private const mcstrfcivpath string = "c:\windows\fciv.exe" public sub testgetfilehash() dim strmyfilepath string dim strmsg string strmyfilepath = excel.application.getopenfilename if strmyfilepath <> "false" strmsg = "md5: " & getfilehash(strmyfilepath, md5) strmsg = strmsg & vbnewline & "sha1: " & getfilehash(strmyfilepath, sha1) msgbox strmsg, vbinformation, "hash of: " & strmyfilepath end if end sub public function getfilehash(byval path string, byval hashtype ehashtype) string dim strrtnval string dim strexec string dim strtemppath string strtemppath = environ$("temp") & "\" & cstr(cdbl(now)) if lenb(dir(strtemppath)) kill strtemppath end if strexec = join(array(environ$("comspec"), "/c", """" & mcstrfcivpath, hashtypetostring(hashtype), """" & path & """", "> " & strtemppath & """")) shell strexec, vbhide if lenb(dir(strtemppath)) strrtnval = getfiletext(strtemppath) end if loop until lenb(strrtnval) strrtnval = split(split(strrtnval, vbnewline)(3))(0) getfilehash = strrtnval end function private function hashtypetostring(byval hashtype string) string dim strrtnval string select case hashtype case ehashtype.md5 strrtnval = "-md5" case ehashtype.sha1 strrtnval = "-sha1" case else err.raise vbobjecterror, "hashtypetostring", "unexpected hash type" end select hashtypetostring = strrtnval end function private function getfiletext(byval filepath string) string dim strrtnval string dim lngfilenum long lngfilenum = freefile open filepath binary access read lngfilenum strrtnval = string$(lof(lngfilenum), vbnullchar) lngfilenum, , strrtnval close lngfilenum getfiletext = strrtnval end function
Comments
Post a Comment