Mega Code Archive

 
Categories / Delphi / Files
 

Ultra Fast File Compare

Title: Ultra Fast File Compare Question: I've a folder full of files, and I want to work out which are the same. Answer: I was once stuck in this situation as I was cleaning out my "Folder Album" Here is the solution I came up with. (note: Non-Recursive) uses PSPcrc; implementation var LookIn, LookFor:String; Filenames,Crcs,Dups : TStringLists; Procedure TForm1.btnBeginClick(Sender: TObject); var sr:Tsearchrec; crc : TPSPcrc; begin Lookin := editLookin.Text; //(eg c:\windows) Lookfor := editLookFor.Text; //(eg *.ini) Filenames := TStringList.Create; Crcs := TStringList.create; Dups := TStringList.Create; crc := TPSPcrc.create(form1); if findfirst(lookin + '\' + lookfor, faAnyFile, sr) = 0 then begin if (sr.Attr and faDirectory) sr.Attr then begin Filenames.Add(sr.name); crc.filename := lookin + '\' + sr.name; crc.calculatecrc; crcs.add(inttohex(crc.crc32,8)); end; while findnext(sr) = 0 do begin if (sr.Attr and faDirectory) sr.Attr then begin crc.filename := lookin + '\' + sr.name; crc.calculatecrc; if crcs.indexof(inttohex(crc.crc32,8)) 0 then dups.add(sr.name) else begin Filenames.add(sr.name); crcs.add(inttohex(crc.crc32,8)); end; end; end; end; findclose(sr); listbox1.items.commatext := filenames.commatext; listbox2.items.commatext := crcs.commatext; listbox3.items.commatext := dups.commatext; filenames.free; crcs.free; dups.free; crc.free; end; Of course you may want to sweeten this up by using TPSPcrc's on progress event... and possibly build a file progress bar again to show current progress through the file system.