Delphi Check File Size

12/11/2017by

If you want to get the file size in bytes for a file, you can use the following function in Delphi/Object Pascal.

Prompted by the first answer I decided to give up on trying to determine when a file copy has completed. Instead I found that using TFileStream gave me a reliable indication whether a file is in use or not.

Delphi Check File Size

Function IsFileInUse(Filename: string; var ResultMessage: string): boolean; var Stream: TFileStream; begin Result:= True; ResultMessage:= '; try Stream:= TFileStream.Create(Filename, fmOpenRead or fmShareDenyWrite); try Result:= False; finally FreeAndNil(Stream); end; Except on E: Exception do ResultMessage:= 'IsFileInUse: ' + E.Message end; end; In this way I can keep on checking until the file is not in use anymore before attempting to process it. It depends on the technique that is used by the copying function. Most copy-methods will allocate the disk space first before they start to copy a file. Python Event Driven Serial. Thus, if you want to copy a file of 4 GB, the system starts by creating a file with random data for 4 GB in total. (Which is done lightning-fast, btw.) It then copies the data itself, but the file size is already what you expect. This has as advantage that the sysmen can check if there's enough disk space available to actually copy the data.

If you write your own file copy function then you can have total control over how it does this. Link Download Plants Vs Zombies 2 Full Cho Pcos more. Else, you're limited to whatever the chosen copy-method offers you. So, how do you copy a file? If you have control over the file copy process, it is easiest to have the copy routine create the file using a temporary filename, and when done, rename it to correct filename. That way, you can use Windows folder monitoring to watch for the renaming (JCL contains a component to help with this, not sure about the name from here).

When your code gets triggered you are sure the other side has finished writing the file. A simple trick I used was to have the copying process create new files with a '$$$' extension. My code still got triggered for those but I ignored them until they were renamed to their proper filename.

Hope this helps.

Comments are closed.