Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delphi compare text file contents

We need to compare the contents of two (or more) text files to determine if we need to create a backup. If they differ we create a new backup.

I currently use the CRC value of each file to check for differences but I was wondering if there is a more efficient or elegant way of detecting differences between to files.

//Use madZIP to calculate the CRC fior this file
GetUncompressedFileInfo(Filename_1, Size_1, NewCRC);

//Use madZIP to calculate the CRC fior this file
GetUncompressedFileInfo(Filename_2, Size_2, OldCRC);

//if ThisFileHash = ExistingFileHash then
if (OldCRC <> NewCRC) then
  CreateABackup;

Regards, Pieter.

like image 893
Pieter van Wyk Avatar asked Dec 27 '22 19:12

Pieter van Wyk


2 Answers

CRC is not a safe method to detect file changes - cryptographic hashes (like MD5 or SHA1) are much better.

Another approach (like the one used by build systems) is to compare file dates. If the file is newer than backup, a new backup is needed.

like image 92
kludg Avatar answered Jan 04 '23 17:01

kludg


CRC is probably more accurate, and pretty efficient. However do you need to check the contents?

I'm assuming you're checking the CRC to see if a modification has been made and re-backup the updated file. In which case FileAge() would do just fine.

like image 24
JamesT Avatar answered Jan 04 '23 15:01

JamesT