给你个参考 下面这个函数用递归的方式,遍历指定目录下的所有文件, 计算出所有文件总的字节长度 写得比较粗糙,不过基本原理就是这样了function GetPathSize(pathname: string): int64; var FindData: TWin32FindData; hf:THandle; tsize:int64; b:boolean; i:integer; tmpstr:string; begin hf := Windows.FindFirstFile(PChar(pathname + '\*.*'), FindData); if hf = INVALID_HANDLE_VALUE then begin Result := 0; exit; end; b := true; tsize := 0; i:=0; while b do begin if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then begin tsize := tsize + FindData.nFileSizeHigh * MAXDWORD + FindData.nFileSizeLow; inc(i); end else begin tmpstr := FindData.cFileName + ''; if (tmpstr <> '.') and (tmpstr <> '..') then begin tsize := tsize + GetPathSize(pathname + '\' + FindData.cFileName); end; end; b := windows.FindNextFile(hf,FindData); end; Result := tsize;end;
FindFirstFile 查 *.* 類型文件就可!
function FileAge(const FileName: string): Integer; var Handle: THandle; FindData: TWin32FindData; LocalFileTime: TFileTime; begin Handle := FindFirstFile(PChar(FileName), FindData); if Handle <> INVALID_HANDLE_VALUE then begin Windows.FindClose(Handle); if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then begin FileTimeToLocalFileTime(FindData.ftLastWriteTime, LocalFileTime); if FileTimeToDosDateTime(LocalFileTime, LongRec(Result).Hi, LongRec(Result).Lo) then Exit; end; end; Result := -1; end; 这是Delphi本身的代码,你看看吧。
试试这个 var Found,Attr:integer; SearchRec:TSearchRec; i:integer; begin i:=0; Found := FindFirst('C:\yourdir\*.*', Attr, SearchRec); while Found = 0 do begin i:=i+1; Found := FindNext(SearchRec); end; FindClose(SearchRec); end;
{ Search record used by FindFirst, FindNext, and FindClose } TSearchRec = record Time: Integer; Size: Integer; Attr: Integer; Name: TFileName; ExcludeAttr: Integer; FindHandle: THandle; FindData: TWin32FindData; end; { FindFirst searches the directory given by Path for the first entry that matches the filename given by Path and the attributes given by Attr. The result is returned in the search record given by SearchRec. The return value is zero if the function was successful. Otherwise the return value is a Windows error code. FindFirst is typically used in conjunction with FindNext and FindClose as follows: Result := FindFirst(Path, Attr, SearchRec); while Result = 0 do begin ProcessSearchRec(SearchRec); Result := FindNext(SearchRec); end; FindClose(SearchRec); where ProcessSearchRec represents user-defined code that processes the information in a search record. }function FindFirst(const Path: string; Attr: Integer; var F: TSearchRec): Integer;{ FindNext returs the next entry that matches the name and attributes specified in a previous call to FindFirst. The search record must be one that was passed to FindFirst. The return value is zero if the function was successful. Otherwise the return value is a Windows error code. }function FindNext(var F: TSearchRec): Integer;{ FindClose terminates a FindFirst/FindNext sequence. FindClose does nothing in the 16-bit version of Windows, but is required in the 32-bit version, so for maximum portability every FindFirst/FindNext sequence should end with a call to FindClose. }procedure FindClose(var F: TSearchRec);{ FileGetDate returns the DOS date-and-time stamp of the file given by Handle. The return value is -1 if the handle is invalid. The FileDateToDateTime function can be used to convert the returned value to a TDateTime value. }
我需要的是这样的函数, function JudgeNum(pathname: string):integer; 它能返回pathname指定的文件夹内的文件数目,或者是 function JudgeExist(pathname: string):boolean; 它能返回pathname指定的文件夹内是否有文件。 因此我不希望代入的参数包括文件的名称.
to: lead001(勇往直前-西安Msn:lead001@) if (tmpstr <> '.') and (tmpstr <> '..') 这名话是什么意思
If FileExists('c:\*.*') then ShowMessage('C:\下有文件') else ShowMessage('C:\下无文件');
/////上次在基础和别人谈过这个问题:(讨论后的结果) function TForm1.IsHasFile(const Path:string):boolean;//返回true代表有文件 var sr: TSearchRec; Attr:integer; iResult:integer; begin result:=false;//初始化,假定没有文件 Attr:=faAnyFile-faDirectory ;//可以为其它属性(具体见Delphi帮助) iResult:= FindFirst(Path+'\*.*',Attr,sr);//检测目录'G:\1\'下面是否有文件 while iResult=0 do begin if (sr.Name <>'.') and (sr.Name <>'..') then //可以去掉 result:=true;//代表有文件 iResult:=FindNext(sr); end; FindClose(sr);end;////////////////// FindFirst()和FindNext()会找到两个不是文件的东东,一是“.”,二是“..”,呵呵,要把这两个排除掉,因为任何一个文件夹,哪怕它是空的,这两个东东也存在
下面这个函数用递归的方式,遍历指定目录下的所有文件,
计算出所有文件总的字节长度
写得比较粗糙,不过基本原理就是这样了function GetPathSize(pathname: string): int64;
var
FindData: TWin32FindData;
hf:THandle;
tsize:int64;
b:boolean;
i:integer;
tmpstr:string;
begin
hf := Windows.FindFirstFile(PChar(pathname + '\*.*'), FindData);
if hf = INVALID_HANDLE_VALUE then
begin
Result := 0;
exit;
end;
b := true;
tsize := 0;
i:=0;
while b do
begin
if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
begin
tsize := tsize + FindData.nFileSizeHigh * MAXDWORD + FindData.nFileSizeLow;
inc(i);
end
else
begin
tmpstr := FindData.cFileName + '';
if (tmpstr <> '.') and (tmpstr <> '..') then
begin
tsize := tsize + GetPathSize(pathname + '\' + FindData.cFileName);
end;
end;
b := windows.FindNextFile(hf,FindData);
end;
Result := tsize;end;
var
Handle: THandle;
FindData: TWin32FindData;
LocalFileTime: TFileTime;
begin
Handle := FindFirstFile(PChar(FileName), FindData);
if Handle <> INVALID_HANDLE_VALUE then
begin
Windows.FindClose(Handle);
if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
begin
FileTimeToLocalFileTime(FindData.ftLastWriteTime, LocalFileTime);
if FileTimeToDosDateTime(LocalFileTime, LongRec(Result).Hi,
LongRec(Result).Lo) then Exit;
end;
end;
Result := -1;
end;
这是Delphi本身的代码,你看看吧。
现在我改变我的需求,就是判断某一文件夹下的文件数目。
var
Found,Attr:integer;
SearchRec:TSearchRec;
i:integer;
begin
i:=0;
Found := FindFirst('C:\yourdir\*.*', Attr, SearchRec);
while Found = 0 do
begin
i:=i+1;
Found := FindNext(SearchRec);
end;
FindClose(SearchRec);
end;
Time: Integer;
Size: Integer;
Attr: Integer;
Name: TFileName;
ExcludeAttr: Integer;
FindHandle: THandle;
FindData: TWin32FindData;
end;
{ FindFirst searches the directory given by Path for the first entry that
matches the filename given by Path and the attributes given by Attr. The
result is returned in the search record given by SearchRec. The return
value is zero if the function was successful. Otherwise the return value
is a Windows error code. FindFirst is typically used in conjunction with
FindNext and FindClose as follows: Result := FindFirst(Path, Attr, SearchRec);
while Result = 0 do
begin
ProcessSearchRec(SearchRec);
Result := FindNext(SearchRec);
end;
FindClose(SearchRec); where ProcessSearchRec represents user-defined code that processes the
information in a search record. }function FindFirst(const Path: string; Attr: Integer;
var F: TSearchRec): Integer;{ FindNext returs the next entry that matches the name and attributes
specified in a previous call to FindFirst. The search record must be one
that was passed to FindFirst. The return value is zero if the function was
successful. Otherwise the return value is a Windows error code. }function FindNext(var F: TSearchRec): Integer;{ FindClose terminates a FindFirst/FindNext sequence. FindClose does nothing
in the 16-bit version of Windows, but is required in the 32-bit version,
so for maximum portability every FindFirst/FindNext sequence should end
with a call to FindClose. }procedure FindClose(var F: TSearchRec);{ FileGetDate returns the DOS date-and-time stamp of the file given by
Handle. The return value is -1 if the handle is invalid. The
FileDateToDateTime function can be used to convert the returned value to
a TDateTime value. }
function JudgeNum(pathname: string):integer;
它能返回pathname指定的文件夹内的文件数目,或者是
function JudgeExist(pathname: string):boolean;
它能返回pathname指定的文件夹内是否有文件。 因此我不希望代入的参数包括文件的名称.
if (tmpstr <> '.') and (tmpstr <> '..') 这名话是什么意思
ShowMessage('C:\下有文件')
else
ShowMessage('C:\下无文件');
也就是说可以写成FileExists('c:\*.txt') {当然也可以使其他扩展名}
但是不能写成FileExists('c:\*.*')所以如果楼主对要判断的目录一无所知的话,还是用FindFirst吧
function TForm1.IsHasFile(const Path:string):boolean;//返回true代表有文件
var
sr: TSearchRec;
Attr:integer;
iResult:integer;
begin
result:=false;//初始化,假定没有文件
Attr:=faAnyFile-faDirectory ;//可以为其它属性(具体见Delphi帮助)
iResult:= FindFirst(Path+'\*.*',Attr,sr);//检测目录'G:\1\'下面是否有文件
while iResult=0 do
begin
if (sr.Name <>'.') and (sr.Name <>'..') then //可以去掉
result:=true;//代表有文件
iResult:=FindNext(sr);
end;
FindClose(sr);end;//////////////////
FindFirst()和FindNext()会找到两个不是文件的东东,一是“.”,二是“..”,呵呵,要把这两个排除掉,因为任何一个文件夹,哪怕它是空的,这两个东东也存在