Mega Code Archive

 
Categories / Delphi / Files
 

Check if a file should be overwritten using version control etc

Title: Check if a file should be overwritten using version control etc. Question: How can I check if a file should be overwritten or not during a copyfile operation? Answer: Below is an example on how you can control if a file should be overwritten or not. Just use: if VersionCopyFileCheck(file1,file2) then CopyFile(file1,file2,FALSE); uses SysUtils, Classes, Windows; // function GetFileVersion(filename : string; var VerBlk : VS_FIXEDFILEINFO) : boolean; var InfoSize,puLen : DWord; Pt,InfoPtr : Pointer; begin InfoSize := GetFileVersionInfoSize(PChar(filename),puLen); fillchar(VerBlk,sizeof(VS_FIXEDFILEINFO),0); if InfoSize 0 then begin GetMem(Pt,InfoSize); GetFileVersionInfo(PChar(filename),0,InfoSize,Pt); VerQueryValue(Pt,'\',InfoPtr,puLen); move(InfoPtr^,VerBlk,sizeof(VS_FIXEDFILEINFO)); FreeMem(Pt); result := true; end else result := false; end; // function VersionCopyFileCheck(Source,Dest : string) : boolean; var SourceVer,DestVer : VS_FIXEDFILEINFO; SourceDate,DestDate : TDateTime; SourceSize,DestSize : integer; begin if FileExists(Dest) then begin if FileExists(Source) then begin if (GetFileVersion(Source,SourceVer) and GetFileVersion(Dest,DestVer)) then begin // Check the version differencies if (SourceVer.dwProductVersionMS if (SourceVer.dwProductVersionMS = SourceVer.dwProductVersionMS) then begin result := (SourceVer.dwProductVersionLS DestVer.dwProductVersionLS); end else result := false; end else result := true; end else begin // There was no version information in the verblock, check the fileage result := (FileAge(Source,SourceVer) FileAge(Dest,DestVer)); end; end else result := false; // No copy, Source doesnt exist end else result := true; // Ok, there is no file that can be overwritten end;