Delphi 获取指定目录下文件的大小
这个函数可以获得指定目录下所有的文件路径+名称,能够递归搜索:
[delphi]
procedure FindAllFile(const Dir: string;List: TStringlist);
var
hFindFile: THandle;
FindFileData: WIN32_FIND_DATA;
FullName,FName,s:string;
begin
s:=IncludeTrailingPathDelimiter(Dir);
hFindFile := FindFirstFile(pchar(s+'*.*'), FindFileData);
if hFindFile <> 0 then begin
repeat
FName:=FindFileData.cFileName;
FullName:=s+FName;
if (FName='.') or (FName='..') then continue;
if (FindFileData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY then
FindAllFile(FullName,List)
else
begin
List.Add(FullName);
end;
until FindNextFile(hFindFile, FindFileData) = false;
windows.FindClose(hFindFile);
end;
end;
procedure FindAllFile(const Dir: string;List: TStringlist);
var
hFindFile: THandle;
FindFileData: WIN32_FIND_DATA;
FullName,FName,s:string;
begin
s:=IncludeTrailingPathDelimiter(Dir);
hFindFile := FindFirstFile(pchar(s+'*.*'), FindFileData);
if hFindFile <> 0 then begin
repeat
FName:=FindFileData.cFileName;
FullName:=s+FName;
if (FName='.') or (FName='..') then continue;
if (FindFileData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY then
FindAllFile(FullName,List)
else
begin
List.Add(FullName);
end;
until FindNextFile(hFindFile, FindFileData) = false;
windows.FindClose(hFindFile);
end;
end;
这个函数可以获得指定文件的大小:
[delphi]
function GetFileSizeByName(AFileName: string): Int64;
var
h: THandle;
dwHigh,dwLow:DWORD;
begin
dwHigh:=0;
if FileExists(AFileName) then
begin
h:= FileOpen(AFileName,fmOpenRead or fmShareDenyNone);
dwLow:=GetFileSize(h,@dwHigh);
if (dwLow = $FFFFFFFF) and (GetLastError() <> NO_ERROR) then
Result:=0
else
Result:= (dwHigh shl 32) + dwLow;
FileClose(h);
end
else
Result := 0;
end;
function GetFileSizeByName(AFileName: string): Int64;
var
h: THandle;
dwHigh,dwLow:DWORD;
begin
dwHigh:=0;
if FileExists(AFileName) then
begin
h:= FileOpen(AFileName,fmOpenRead or fmShareDenyNone);
dwLow:=GetFileSize(h,@dwHigh);
if (dwLow = $FFFFFFFF) and (GetLastError() <> NO_ERROR) then
Result:=0
else
Result:= (dwHigh shl 32) + dwLow;
FileClose(h);
end
else
Result := 0;
end;
要获得目录的总大小,可以先用第一个函数获得所有目录下文件的列表,然后遍历这个列表,累加文件尺寸。
摘自 #Define
补充:软件开发 , Delphi ,