有没有这样的API函数,可以判断某一个文件夹下是否有文件(不论文件的大小是否为零都认为该文件存在)?
解决方案 »
- delphi7 TRzGroup控件增加item,在开发状态能显示出来,但是编译运行之后就没有显示
- *****有关DELPHI截取字符的问题*****
- 一个关于 Remoto Data Module 的问题
- 求InstallShield for delphi6,急!!
- 将字符(如:名字,年龄,年月等)转化为16进制该怎么转???
- 关于用delphi sql sever 做的c/s模式应用程序的问题求助!
- 问一个菜菜的关于操作文本的问题
- 谁知道怎样用刻录机把文件保存为*.iso格式的文件。急!!!!!!!!!!!!
- 关于恢复数据库的操作,总是报错!高手请进来帮帮我!急急急急急急急急急急急急急急急急!
- 关于Memo的东西来考考大家(不要小看这个问题哦,说不定你也用得上),有兴趣的来看看(962veiri)
- 线程、创建视图、进度条
- 我有以下几方面的困惑,想请教大家
下面这个函数用递归的方式,遍历指定目录下的所有文件,
计算出所有文件总的字节长度
写得比较粗糙,不过基本原理就是这样了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()会找到两个不是文件的东东,一是“.”,二是“..”,呵呵,要把这两个排除掉,因为任何一个文件夹,哪怕它是空的,这两个东东也存在