Comparing sizes of files in Delphi

Delphi has many system routines that enable you to handle and work on files and storage contents. Also, these routines can be used on all supported platforms, provided by the System unit and sub-units.

You can use these routines to create, access, modify, update and remove files and their contents, and even their meta-data.

But, unfortunately, Delphi has no ready-to-use function to compare sizes of two or more files. You may need it to compare assets files or in the update process of your app.

So, in this tutorial, we will show you how to implement your own function to compare size of files.

First, let’s create a class function to get file size

To get the size of a file in your storage, you must assign a file variable to this file. Then, you can use this handler to access your file by reading and writing. And you can get special meta-data like size.

function FileSize(AFileName: string): integer;
var
AFile : file;
ACh : char;
begin
AssignFile(AFile, AFileName);
Reset(AFile, 1);
Result := System.FileSize(AFile);
CloseFile(AFile);
end;

Next, create a custom function to compare files size

Using the previous function, you can now create a function that receive files’s path string and return a TValueRelationship value about their sizes. Without worring about file assigning.

function CompareFileSize(AFileName1,
  AFileName2: string): TValueRelationship;
begin
Result := CompareValue(FileSize(AFileName1), FileSize(AFileName2))
end;

See also

Leave a Reply