delphi中如何得到某个文件夹下某些后缀文件的列表(不包含子文件夹)?比如说返回newfoler下所有.jpg和.png文件的列表。

解决方案 »

  1.   

    用Sample页的ShellListView控件,ViewSytle设成vsReport,在OnAddFolder事件写:procedure TForm1.ShellListView1AddFolder(Sender: TObject;
      AFolder: TShellFolder; var CanAdd: Boolean);
    var Ext:string;
    begin
      Ext:=ExtractFileExt(AFolder.PathName);
      if (Ext='.jpg')or(ext='.png') then CanAdd:=True
       else CanAdd:=False;
    end;
      

  2.   

    谢谢楼上的两位,我现在的情况是这样的
    用一个opendialog选择一个路径,如果只指到文件夹,则要将这个文件夹中符合条件的图片路径全部写到一个数组中,
    不知道怎么弄。
      

  3.   

    这是我的第一贴,呵呵!
    就用楼上那位说的那两个API,用递归的方法可以历遍某个目录及其字目录下面所有的文件。另外,要对查找文件的类型有所限制,可以用GetFileAttributes。
    提醒一下,这样搜索文件占用大量系统资源,要是某个目录非常大的话,搜索要花费很长时间,应该用一个独立线程去执行查找,不然机器会假死。
      

  4.   


    使用Findfirst和Findnext很方便的呀。这又涉及不到什么递归。(楼主也就是搜某个目录下的文件,他都说了不包括子目录了)
      

  5.   

    if not DirectoryExists(sp) then
      begin
        lbox_result.Items.Add('Can''t find source folder!');
        edt_spath.Enabled := true;
        btn_start.Enabled := true;
        exit;
      end;  if FindFirst(sp + '\*.*',faDirectory,fd) = 0 then
      begin
        fn := fd.Name;
        if fn <> '.' then
          lbox_sp.Items.Add(fn);
          lbox_sp.Refresh;
        while FindNext(fd) = 0 do
        begin
          if fd.Attr <> 16 then
          begin
            FindClose(fd);
            edt_spath.Enabled := true;
            btn_start.Enabled := true;
            lbox_result.Items.Add('Can''t find source folder!');
            exit;
          end
          else
          begin
            fn := fd.Name;
            if fn <> '..' then
              lbox_sp.Items.Add(fn);
          end;
        end;
      end;
      

  6.   

    以上为遍列所有文件夹
    以下为遍列所有文件
      sp := Trim(edt_spath.Text);
      if not DirectoryExists(sp) then
      begin
        lbox_result.Items.Add('Can''t find source folder!');
        edt_spath.Enabled := true;
        btn_start.Enabled := true;
        exit;
      end;  if FindFirst(sp + '\*.*',faAnyFile,fd) = 0 then
      begin
        fn := fd.Name;
        if fn <> '.' then
          lbox_sp.Items.Add(fn);
          lbox_sp.Refresh;
        while FindNext(fd) = 0 do
        begin
          if fd.Attr <> 32 then
          begin
            FindClose(fd);
            edt_spath.Enabled := true;
            btn_start.Enabled := true;
            lbox_result.Items.Add('Can''t find source folder!');
            exit;
          end
          else
          begin
            fn := fd.Name;
            if fn <> '..' then
              lbox_sp.Items.Add(fn);
          end;
        end;
      end;