function Get_File_Size1(sFileToExamine: string; bInKBytes: Boolean): string; { for some reason both methods of finding file size return a filesize that is slightly larger than what Windows File Explorer reports } var FileHandle: THandle; FileSize: LongWord; d1: Double; i1: Int64; begin //a- Get file size FileHandle := CreateFile(PChar(sFileToExamine), GENERIC_READ, 0, {exclusive} nil, {security} OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); FileSize := GetFileSize(FileHandle, nil); Result := IntToStr(FileSize); CloseHandle(FileHandle); //a- optionally report back in Kbytes if bInKbytes = True then begin if Length(Result) > 3 then begin Insert('.', Result, Length(Result) - 2); d1 := StrToFloat(Result); Result := IntToStr(round(d1)) + 'KB'; end else Result := '1KB'; end; end; {****************************************************************************** Thanks to Advanced Delphi Systems here's another method which works just as well returning the same results *******************************************************************************} function Get_File_Size2(sFileToExamine: string; bInKBytes: Boolean): string; var SearchRec: TSearchRec; sgPath: string; inRetval, I1: Integer; begin sgPath := ExpandFileName(sFileToExamine); try inRetval := FindFirst(ExpandFileName(sFileToExamine), faAnyFile, SearchRec); if inRetval = 0 then I1 := SearchRec.Size else I1 := -1; finally SysUtils.FindClose(SearchRec); end; Result := IntToStr(I1); end; procedure TForm1.Button1Click(Sender: TObject); begin if OpenDialog1.Execute then label1.Caption := Get_File_Size(Opendialog1.FileName, True); end; {*******************************************************************************} function Get_File_Size3(const FileName: string): TULargeInteger; // by nico var Find: THandle; Data: TWin32FindData; begin Result.QuadPart := -1; Find := FindFirstFile(PChar(FileName), Data); if (Find <> INVALID_HANDLE_VALUE) then begin Result.LowPart := Data.nFileSizeLow; Result.HighPart := Data.nFileSizeHigh; Windows.FindClose(Find); end; end; procedure TForm1.Button1Click(Sender: TObject); begin if (OpenDialog1.Execute) then ShowMessage(IntToStr(Get_File_Size3(OpenDialog1.FileName).QuadPart)); end; {*******************************************************************************} function Get_File_Size4(const S: string): Int64; var FD: TWin32FindData; FH: THandle; begin FH := FindFirstFile(PChar(S), FD); if FH = INVALID_HANDLE_VALUE then Result := 0 else try Result := FD.nFileSizeHigh; Result := Result shl 32; Result := Result + FD.nFileSizeLow; finally CloseHandle(FH); end; end;
何必这么麻烦,用这个函数就可以function GetDirectorySize(const ADirectory: string): Integer; var Dir: TSearchRec; Ret: integer; Path: string; begin Result := 0; Path := ExtractFilePath(ADirectory); Ret := Sysutils.FindFirst(ADirectory, faAnyFile, Dir); if Ret <> NO_ERROR then exit; try while ret = NO_ERROR do begin inc(Result, Dir.Size); if (Dir.Attr in [faDirectory]) and (Dir.Name[1] <> '.') then Inc(Result, GetDirectorySize(Path + Dir.Name + '\*.*')); Ret := Sysutils.FindNext(Dir); end; finally Sysutils.FindClose(Dir); end; end;
同意: 何必这么麻烦,用这个函数就可以function GetDirectorySize(const ADirectory: string): Integer; var Dir: TSearchRec; Ret: integer; Path: string; begin Result := 0; Path := ExtractFilePath(ADirectory); Ret := Sysutils.FindFirst(ADirectory, faAnyFile, Dir); if Ret <> NO_ERROR then exit; try while ret = NO_ERROR do begin inc(Result, Dir.Size); if (Dir.Attr in [faDirectory]) and (Dir.Name[1] <> '.') then Inc(Result, GetDirectorySize(Path + Dir.Name + '\*.*')); Ret := Sysutils.FindNext(Dir); end; finally Sysutils.FindClose(Dir); end; end;
正好前几天要用,作了一个。 uses ShellApifunction GetDirectorySize(Path: String): Integer; //eg. Path = 'c:\temp\' var SR: TSearchRec; begin Result := 0; if FindFirst(Path + '*.*', faAnyFile, SR) = 0 then begin if (sr.Name <> '.') and (sr.Name <> '..') and (sr.Attr = faDirectory) then Result := Result + GetDirectorySize(Path+Sr.Name+'\') else Result := Result + Sr.Size; while FindNext(sr) = 0 do if (sr.Name <> '.') and (sr.Name <> '..') and (sr.Attr = faDirectory) then Result := Result + GetdirectorySize(Path+Sr.Name+'\') else Result := Result + Sr.Size; FindClose(sr); end; end;
function GetDirectorySize(Path: String): Integer; //eg. Path = 'c:\temp\' var SR: TSearchRec; begin Result := 0; if FindFirst(Path + '*.*', faAnyFile, SR) = 0 then begin if (sr.Name <> '.') and (sr.Name <> '..') and (sr.Attr = faDirectory) then Result := Result + GetDirectorySize(Path+Sr.Name+'\') else Result := Result + Sr.Size; while FindNext(sr) = 0 do if (sr.Name <> '.') and (sr.Name <> '..') and (sr.Attr = faDirectory) then Result := Result + GetdirectorySize(Path+Sr.Name+'\') else Result := Result + Sr.Size; FindClose(sr); end; end;
{
for some reason both methods of finding file size return
a filesize that is slightly larger than what Windows File
Explorer reports
}
var
FileHandle: THandle;
FileSize: LongWord;
d1: Double;
i1: Int64;
begin
//a- Get file size
FileHandle := CreateFile(PChar(sFileToExamine),
GENERIC_READ,
0, {exclusive}
nil, {security}
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
FileSize := GetFileSize(FileHandle, nil);
Result := IntToStr(FileSize);
CloseHandle(FileHandle);
//a- optionally report back in Kbytes
if bInKbytes = True then
begin
if Length(Result) > 3 then
begin
Insert('.', Result, Length(Result) - 2);
d1 := StrToFloat(Result);
Result := IntToStr(round(d1)) + 'KB';
end
else
Result := '1KB';
end;
end; {******************************************************************************
Thanks to Advanced Delphi Systems here's another method which works just as
well returning the same results
*******************************************************************************}
function Get_File_Size2(sFileToExamine: string; bInKBytes: Boolean): string;
var
SearchRec: TSearchRec;
sgPath: string;
inRetval, I1: Integer;
begin
sgPath := ExpandFileName(sFileToExamine);
try
inRetval := FindFirst(ExpandFileName(sFileToExamine), faAnyFile, SearchRec);
if inRetval = 0 then
I1 := SearchRec.Size
else
I1 := -1;
finally
SysUtils.FindClose(SearchRec);
end;
Result := IntToStr(I1);
end; procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
label1.Caption := Get_File_Size(Opendialog1.FileName, True);
end; {*******************************************************************************} function Get_File_Size3(const FileName: string): TULargeInteger;
// by nico
var
Find: THandle;
Data: TWin32FindData;
begin
Result.QuadPart := -1;
Find := FindFirstFile(PChar(FileName), Data);
if (Find <> INVALID_HANDLE_VALUE) then
begin
Result.LowPart := Data.nFileSizeLow;
Result.HighPart := Data.nFileSizeHigh;
Windows.FindClose(Find);
end;
end; procedure TForm1.Button1Click(Sender: TObject);
begin
if (OpenDialog1.Execute) then
ShowMessage(IntToStr(Get_File_Size3(OpenDialog1.FileName).QuadPart));
end; {*******************************************************************************} function Get_File_Size4(const S: string): Int64;
var
FD: TWin32FindData;
FH: THandle;
begin
FH := FindFirstFile(PChar(S), FD);
if FH = INVALID_HANDLE_VALUE then Result := 0
else
try
Result := FD.nFileSizeHigh;
Result := Result shl 32;
Result := Result + FD.nFileSizeLow;
finally
CloseHandle(FH);
end;
end;
var
Dir: TSearchRec;
Ret: integer;
Path: string;
begin
Result := 0;
Path := ExtractFilePath(ADirectory);
Ret := Sysutils.FindFirst(ADirectory, faAnyFile, Dir);
if Ret <> NO_ERROR then exit;
try
while ret = NO_ERROR do
begin
inc(Result, Dir.Size);
if (Dir.Attr in [faDirectory]) and (Dir.Name[1] <> '.') then
Inc(Result, GetDirectorySize(Path + Dir.Name + '\*.*'));
Ret := Sysutils.FindNext(Dir);
end;
finally
Sysutils.FindClose(Dir);
end;
end;
何必这么麻烦,用这个函数就可以function GetDirectorySize(const ADirectory: string): Integer;
var
Dir: TSearchRec;
Ret: integer;
Path: string;
begin
Result := 0;
Path := ExtractFilePath(ADirectory);
Ret := Sysutils.FindFirst(ADirectory, faAnyFile, Dir);
if Ret <> NO_ERROR then exit;
try
while ret = NO_ERROR do
begin
inc(Result, Dir.Size);
if (Dir.Attr in [faDirectory]) and (Dir.Name[1] <> '.') then
Inc(Result, GetDirectorySize(Path + Dir.Name + '\*.*'));
Ret := Sysutils.FindNext(Dir);
end;
finally
Sysutils.FindClose(Dir);
end;
end;
uses ShellApifunction GetDirectorySize(Path: String): Integer; //eg. Path = 'c:\temp\'
var
SR: TSearchRec;
begin
Result := 0;
if FindFirst(Path + '*.*', faAnyFile, SR) = 0 then
begin
if (sr.Name <> '.') and (sr.Name <> '..') and (sr.Attr = faDirectory) then
Result := Result + GetDirectorySize(Path+Sr.Name+'\')
else
Result := Result + Sr.Size;
while FindNext(sr) = 0 do
if (sr.Name <> '.') and (sr.Name <> '..') and (sr.Attr = faDirectory) then
Result := Result + GetdirectorySize(Path+Sr.Name+'\')
else
Result := Result + Sr.Size;
FindClose(sr);
end;
end;
function GetDirectorySize(Path: String): Integer; //eg. Path = 'c:\temp\'
var
SR: TSearchRec;
begin
Result := 0;
if FindFirst(Path + '*.*', faAnyFile, SR) = 0 then
begin
if (sr.Name <> '.') and (sr.Name <> '..') and (sr.Attr = faDirectory) then
Result := Result + GetDirectorySize(Path+Sr.Name+'\')
else
Result := Result + Sr.Size;
while FindNext(sr) = 0 do
if (sr.Name <> '.') and (sr.Name <> '..') and (sr.Attr = faDirectory) then
Result := Result + GetdirectorySize(Path+Sr.Name+'\')
else
Result := Result + Sr.Size;
FindClose(sr);
end;
end;