有没有这样的API函数,可以判断某一个文件夹下是否有文件(不论文件的大小是否为零都认为该文件存在)?

解决方案 »

  1.   

    试试 Function FileExsits(const FileName);
      

  2.   

    给你个参考
    下面这个函数用递归的方式,遍历指定目录下的所有文件,
    计算出所有文件总的字节长度
    写得比较粗糙,不过基本原理就是这样了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;
      

  3.   

    FindFirstFile 查 *.* 類型文件就可!
      

  4.   

    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本身的代码,你看看吧。
      

  5.   

    我需要的是判断一个文件夹内是否有文件,因此代入的参数应该为一个路径而不是文件名,在这一点上我同意lead001的作法,但我想不能通过判断该文件夹内的文件总的字节长度是否为零来决定,因为它完全可以有一个空的文本文件,使用Windows的属性功能查之其大小为零,在这个意义上我认为lead001的作法又不可取,当然我会先进行一下测试。
      

  6.   

    lead001的作法我已经进行了测试,的确可以统计出某一文件夹下所有文件的字节数。
       现在我改变我的需求,就是判断某一文件夹下的文件数目。
      

  7.   

    试试这个
    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;
      

  8.   

    { 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. }
      

  9.   

    我需要的是这样的函数,
       function JudgeNum(pathname: string):integer;
         它能返回pathname指定的文件夹内的文件数目,或者是
       function JudgeExist(pathname: string):boolean;
         它能返回pathname指定的文件夹内是否有文件。   因此我不希望代入的参数包括文件的名称.
      

  10.   

    to: lead001(勇往直前-西安Msn:lead001@) 
    if (tmpstr <> '.') and (tmpstr <> '..') 这名话是什么意思
      

  11.   

    If FileExists('c:\*.*') then
      ShowMessage('C:\下有文件')
    else
      ShowMessage('C:\下无文件');
      

  12.   

    楼上的不行,FileExists支持文件名的通配符,但是扩展名不可以,
    也就是说可以写成FileExists('c:\*.txt') {当然也可以使其他扩展名}
    但是不能写成FileExists('c:\*.*')所以如果楼主对要判断的目录一无所知的话,还是用FindFirst吧
      

  13.   

    /////上次在基础和别人谈过这个问题:(讨论后的结果)
    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()会找到两个不是文件的东东,一是“.”,二是“..”,呵呵,要把这两个排除掉,因为任何一个文件夹,哪怕它是空的,这两个东东也存在