Re: Hash function
- To: mathgroup at smc.vnet.net
- Subject: [mg97527] Re: Hash function
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Sat, 14 Mar 2009 18:14:52 -0500 (EST)
On 3/14/09 at 5:41 AM, Ignacio.Plazeta at speednet.es (Ignacio Plazeta) wrote: >I bet I'm wrong in a childish way but I'm incapable to overcome the >bug. >Why does the following code return discrepant values when processing >two identical file ( CRTL + C , CRTL + V ) placed in two different >folders ? Of course, I'm looking on only the Hash part of the out, >being obviously the folder related part. >Warmest regards. Ignacio >fileIdentifier[myFile_]:=Module[{ tmp , folderPos }, >folderPos = StringPosition[myFile,"\\"]//Flatten//Max; >tmp={ >FileByteCount[myFile], >Hash[myFile,"SHA512"], >StringJoin[ >(If[#>10,ToString[#],"0"<>ToString[#]])&/@FileDate[myFile] >], >StringTake[myFile,folderPos-1], >StringDrop[myFile,folderPos] >}; >Return[tmp]; >] The key issue here is you want to use FileHash not Hash for working with files. Additionally, your code can be made platform independent and more robust by making more use of built in functions. That is: fileID[filename_] := Module[{dirName = DirectoryName@filename}, {FileByteCount[filename], FileHash[filename, "SHA512"], DateString[ FileDate@filename, {"Year", "Month", "Day", "Hour", "Minute", "Second"}], StringDrop[dirName, -1], StringDrop[filename, StringLength@dirName]}] accomplishes the same thing as your code without the bug and without need to know what character is used as a path name separator.