高手能否讲讲,或者给个例程?

解决方案 »

  1.   

    procedure TForm1.FindAll(sPath,sFileName:string;lstView:TListView);
    var
      FindData:TWin32FindData;
      hFindfile:Cardinal;
      tFile:string;
      NewItem:TListItem;
    begin
      if sPath[Length(sPath)]<>'\' then
        sPath:=sPath+'\';
      hFindfile:=FindFirstFile(pchar(sPath+sFileName),FindData);
      if hFindfile=Invalid_Handle_Value then
        Exit;
      repeat
        tFile:=FindData.cFileName;
        if tFile[1]='.' then  //如果是 .. 或 .则从头开始
            Continue;
        if FindData.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY then
          FindAll(spath+tfile+'\',sFileName,lstView)
        else begin
          NewItem:=lstView.Items.Add;
          NewItem.Caption:=tFile;
          NewItem.SubItems.Add(sPath);
          NewItem.SubItems.Add(IntToStr(FindData.nFileSizeLow));
        end;  until FindNextFile(hFindfile,FindData)=False;
      Windows.FindClose(hFindfile);
    end;
    procedure TForm1.btn3Click(Sender: TObject);
    begin
      Findall('C:\windows\system32','*.*',lv1);
    end;
      

  2.   

    procedure GetList(SLFile:TStringList;FPath:String);
    var sr: TSearchRec;
        FileAttrs: Integer;
        Ret:Integer;
    begin
      FileAttrs := faReadOnly + faHidden+ faSysFile+ faVolumeID+ faDirectory+faArchive+faAnyFile;
      Ret:=FindFirst(FPath+'*.*', FileAttrs, sr);
      while Ret = 0 do begin
        //是目录
        if (sr.Attr and faDirectory) = faDirectory then begin
          if (sr.Name<>'.') and (sr.Name<>'..') then  begin
            GetPromptVoxList(SLFile,FPath+sr.Name+'\');
          end;
        end else begin
          //是文件
          SLFile.Add(FPath+sr.Name);
        end;
        Ret:=FindNext(sr);
      end;
      FindClose(sr);
    end;
      

  3.   

    不行  cncharles(旺仔)和 belllab(菜鸟)的方法我都试过,都不行cncharles(旺仔)的是有明显的逻辑错误:先有一句"tFile:=FindData.cFileName;",再来一句"FindAll(spath+tfile+'\',sFileName,lstView)"这样的递归,就会出现把文件当作文件夹来处理,会把C:\temp\*.xls也加上个'\'作为文件夹目录参数传递给FindAll()函数,这样会报错.belllab(菜鸟) 的是根本就不能执行, GetPromptVoxList(SLFile,FPath+sr.Name+'\')函数可能是GetList()函数以实现递归,但更改成GetList()也不能达成目的.'SLFile.Add(FPath+sr.Name);'这条指令就没有见它执行过.
    ------------------------------------------------------
    我认为现在的核心问题的应该怎么样判断某一条item是文件还是文件夹.下面是FTP下载的过程,它会根据选中的是文件还是文件夹进行不同的处理.使用的idFtp组件.procedure TFTPclients.Btn_downloadClick(Sender: TObject);
    var
    strpath:string;
    begin
        if not CreateDirectory(Edt_path.Text) then
        begin
            showmessage('文件夹未'+Edt_path.Text+'新建成功');
        end;
        if FTPclient.Connected  then
        begin
            FTPclient.List(nil);
            if FTPclient.DirectoryListing[lbx_face.ItemIndex].ItemType = ditDirectory then
            downloadDir(FTPclient,Edt_Crtpath.Text,Edt_path.Text)
            else
            begin
            strpath:= edt_path.Text+FTPclient.DirectoryListing[lbx_face.ItemIndex].FileName;
            FTPclient.Get(FTPclient.DirectoryListing[lbx_face.ItemIndex].FileName,strpath,true);
            application.MessageBox('下载成功','提示',0);
            end;
        end;
    end;
    -------------------------------------
    FTPclient.DirectoryListing[lbx_face.ItemIndex].ItemType 我们注意到它有个属性来记录ITEM是文件夹还是文件,这样我们操作起来就超级简单了.所以,哪位高手能帮我解决怎样判断item是文件夹还是文件,这个问题就解决了,不知道哪位高手有什么高招,谢谢
      

  4.   

    怎么可能呢, 我自己写的都调试的没有问题的.你在窗体上放一个TTreeList名称为lv1, 放一个Button名称btn3
    用下面的代码测试;type
      TForm1 = class(TForm)
        lv1: TListView;
        btn3: TButton;
        procedure btn3Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        procedure FindAll(sPath,sFileName:string;lstView:TListView);
      public
        { Public declarations }
      end;
    procedure TForm1.FormCreate(Sender: TObject);
    var
      NewColumn:TListColumn;
    begin
     with lv1 do begin
        if Assigned(Columns) then
          Columns.Clear;
        if Assigned(Items) then
          Items.Clear;
        NewColumn:=Columns.Add;
        NewColumn.Caption:='File Name';
        NewColumn.Width:=100;
        NewColumn:=Columns.Add;
        NewColumn.Caption:='File Path';
        NewColumn.Width:=300;
        NewColumn:=Columns.Add;
        NewColumn.Caption:='File Size';
        NewColumn.Width:=50;
      end;
    end;procedure TForm1.FindAll(sPath,sFileName:string;lstView:TListView);
    var
      FindData:TWin32FindData;
      hFindfile:Cardinal;
      tFile:string;
      NewItem:TListItem;
    begin
      if sPath[Length(sPath)]<>'\' then
        sPath:=sPath+'\';
      hFindfile:=FindFirstFile(pchar(sPath+sFileName),FindData);
      if hFindfile=Invalid_Handle_Value then
        Exit;
      repeat
        tFile:=FindData.cFileName;
        if tFile[1]='.' then  //如果是 .. 或 .则从头开始
            Continue;
        if FindData.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY then
          FindAll(spath+tfile+'\',sFileName,lstView)
        else begin
          NewItem:=lstView.Items.Add;
          NewItem.Caption:=tFile;
          NewItem.SubItems.Add(sPath);
          NewItem.SubItems.Add(IntToStr(FindData.nFileSizeLow));
        end;  until FindNextFile(hFindfile,FindData)=False;
      Windows.FindClose(hFindfile);
    end;procedure TForm1.btn3Click(Sender: TObject);
    begin
      Findall('C:\windows','*.*',lv1);
    end;
      

  5.   

    procedure GetList(SLFile:TStringList;FPath:String);
    var sr: TSearchRec;
        FileAttrs: Integer;
        Ret:Integer;
    begin
      FileAttrs := faReadOnly + faHidden+ faSysFile+ faVolumeID+ faDirectory+faArchive+faAnyFile;
      Ret:=FindFirst(FPath+'*.*', FileAttrs, sr);//注意这里,要求传入的目录名最后一个字符是\号,可能
      while Ret = 0 do begin
        //是目录
        if (sr.Attr and faDirectory) = faDirectory then begin
          if (sr.Name<>'.') and (sr.Name<>'..') then  begin
            GetList(SLFile,FPath+sr.Name+'\');
          end;
        end else begin
          //是文件
          SLFile.Add(FPath+sr.Name);
        end;
        Ret:=FindNext(sr);
      end;
      FindClose(sr);
    end;
      

  6.   

    DEMOprocedure TForm1.Button1Click(Sender: TObject);
    var SL1:TStringList;
    begin
      SL1:=TStringList.Create;
      GetList(SL1,'C:\Downloads\');
      Memo1.Lines.AddStrings(sl1);
      sl1.Free;
    end;