這個函數可以獲得指定目錄下所有的文件路徑+名稱,能夠遞歸搜索:
[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