2013年1月28日 星期一

Delphi 學習筆記偵測檔案夾的檔案個數


如何偵測檔案夾的檔案個數

function FileCount(filePath:string):integer;
var pathstr:string;
    sr: TSearchRec;
    FileAttrs: Integer;
begin
    pathstr:=GetCurrentDir;
    pathstr:=pathstr+'\sqlFile\*.*';
    Form1.Edit1.Text:=pathstr;
    FileAttrs := FileAttrs + faAnyFile;
    FileCount:=0;
    // FindFirst returns 0 if a file was successfully located, otherwise, it returns an //error code.

    if SysUtils.FindFirst('K:\torch\sqlFile\*.*', FileAttrs, sr) = 0 then
    begin
            with Form1.stringgrid1 do
            begin
                rowCount:=1;
                repeat
                if (sr.Attr and FileAttrs) = sr.Attr then
                begin
                   RowCount := RowCount + 1;
                   Cells[1,RowCount-1] := sr.Name;
                   Cells[2,RowCount-1] := IntToStr(sr.Size);
                   inc(FileCount);
                end;
//FindNext returns 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 an //error code.            
    until FindNext(sr) <> 0;
            end;
    end;
end;

沒有留言:

張貼留言