var
fs: tFileStream;
filesize_: int64;
begin
fs := TFileStream.Create('C:\som
try filesize_ := fs.Size; finally fs.free; end;
label1.caption := inttostr(filesize_);
end;
Main Topics
Browse All TopicsDear all,
i am using delphi 6 and i want to get the file size for any file located in my hard drive or my network as i assigend the path and file name to the variable.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
2 things that in some cases produce "logical" errors and are probably among the hardest bugs to track down.
one addition to kiranmahales code:
- since filemode is a global variable, you might want to save initial value and restore it otherwise you might encounter issues later on in your project (happened to me)
one addition to lokis code:
- flags are usually ORed not +ed. even though at first sign adding them get the same result, one might wrongfully use addition out of habbit in a case where this is not happening (for example when the flag is already defined, the value is not known precisely, but another flag must be made available)
in this particular case, it's ok to use addition, but keep in mind not to get use to it and better use ORing just to be on the safe side
Hi,
Both examples above open the file. The following is another example:
function GetFileSize(const strFile: string): Int64;
var
h: THandle;
FindData:TWin32FindData;
begin
result := 0;
h := windows.FindFirstFile(PCha
if (h > 0) then begin
// File found.
result := (Int64(FindData.nFileSizeH
end;
windows.FindClose(h);
end;
Regards, Geo
function Get_File_Size2(sFileToExam
var
SearchRec: TSearchRec;
sgPath: string;
inRetval, I1: Integer;
begin
sgPath := ExpandFileName(sFileToExam
try
inRetval := FindFirst(ExpandFileName(s
if inRetval = 0 then
I1 := SearchRec.Size
else
I1 := -1;
finally
SysUtils.FindClose(SearchR
end;
Result := IntToStr(I1);
end;
procedure TForm1.Button1Click(Sender
begin
if OpenDialog1.Execute then
label1.Caption := Get_File_Size(Opendialog1.
end;
You can use GetFileAttributesEx
http://windowssdk.msdn.mic
Advandages:
*) It does not open the file
*) It does not traverse a directory to find the file
*) It returns 2 DWORDS for the file size, so it supports files larger than 4 Gb.
Function GetMyNewFileSize(fnm: string): Int64;
var
srec: TSearchRec;
begin
// resolves file size to 64bits instead of just 32bits (as some other methods)
// AND (on my computer) it is more than twice as fast as methods which cause the file to be opened
result:=0;
if SysUtils.FindFirst(fnm,faA
begin
result:=Int64((srec.FindDa
SysUtils.FindClose(srec);
end;
end;
Business Accounts
Answer for Membership
by: kiranmahalePosted on 2006-08-29 at 13:33:44ID: 17414965
function TForm1.AnyFileSize(const FileName : string ): Int64;
const BLOCK_SIZE=1024;
var BytesRead, BytesToWrite, Count : integer;
F : FIle of Byte;
pTemp : Pointer;
begin
Result := 0;
If FileExists(FileName) then
begin
AssignFile( F, FileName );
FileMode := 0; {Access file in read only mode}
Reset(F);
try
Result := FileSize( F );
finally
CloseFile( F );
end;
end;
end;