delphi  如何获取同一目录下指定扩展名 的文件名

解决方案 »

  1.   

    var
      tls: TStringList;
      FindFileData: TWIN32FindData;
      FindHanle: THandle;
      FilePath, FileExt: String;
    begin
      tls := TStringList.Create;
      try
        FillChar(FindFileData, SizeOf(TWIN32FindData), 0);
        FilePath := 'x:\dir\';  //目录
        FileExt := '.ext';      //扩展名
        FindHanle := FindFirstFile(PAnsiChar(FilePath + '*' + FileExt), FindFileData);
        if FindHanle <> INVALID_HANDLE_VALUE then begin
          repeat
            if (FindFileData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) <> FILE_ATTRIBUTE_DIRECTORY then begin
              //过滤掉属性为“目录”,即“文件”
              tls.Add(FilePath + FindFileData.cFileName);
            end;
          until Not FindNextFile(FindHanle, FindFileData) ;
          windows.FindClose(FindHanle);
        end;
        Memo1.Lines.Assign(tls);
      finally
        tls.Free;
      end;
    end;
      

  2.   


    procedure TForm1.Getfile(Directory,extname: string;var str:TStrings);
    var
      SearchRec: TSearchRec;
      s:string;
    begin
      if Directory[Length(Directory)] <> '\' then
        Directory := Directory + '\';
      if FindFirst(Directory + '*.*' , faAnyFile, SearchRec) = 0 then
      begin
        repeat
          if (SearchRec.Attr and fadirectory=fadirectory) and (SearchRec.Name <> '..') and (SearchRec.Name <> '.') then
          begin
          end
          else
          begin
            s:=ExtractFileExt(SearchRec.Name);
            if s=extname then
              str.Add(Directory+SearchRec.Name);
          end;
        until FindNext(SearchRec) <> 0;
        FindClose(SearchRec);
      end;
    end;
      

  3.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
    F: File;
    begin
      ListBox1.Items := MakeFileList(ExtractFilePath(Application.Exename),'.tmp');
      Edit1.Text := ListBox1.Items.Text;
    AssignFile(F, (ExtractFilePath(Application.Exename)+'!Llst.ini'));ReName(F, (ExtractFilePath(Application.Exename)+(Edit1.Text)));
      end;那里错了 求解本人菜鸟
      

  4.   

    说说你这段代码什么错误啊makefilelist是自定义函数吧
      

  5.   

    ReName(F, (ExtractFilePath(Application.Exename)+(Edit1.Text)));   这句中的(Edit1.Text)  这个  用字符串   就行 '43434'  这样可以  (Edit1.Text)  这样出问题
      

  6.   

    我试了没问题啊你用edit1.text的话出什么错?
      

  7.   

      //Find avialable File
      vStringList := TStringList.Create;
      sName:= 'D:\Dir\';
      if FindFirst(sName+'*.db', faAnyFile and not faDirectory, vSearchRec)=0 then
      begin
        repeat
          vStringList.Add(vSearchRec.Name);
        until FindNext(vSearchRec) <> 0;
        FindClose(vSearchRec);
      end;
      ComboBox1.Clear;
      ComboBox1.Items.AddStrings(vStringList);
      ComboBox1.Refresh;
      vStringList.Free;
      

  8.   

    function GetFolderList(Path,FileExt:string):TStringList;
    var
      sch:TSearchrec;
      isSearchfolder:Boolean;
    begin
      if FileExt='\\' then isSearchfolder:=true else isSearchfolder:=false;
      Result:=TStringlist.Create;
      if rightStr(trim(Path), 1) <> '\' then
        Path := trim(Path) + '\'
      else
        Path := trim(Path);
      if not DirectoryExists(Path) then
      begin
        Result.Clear;
        exit;
      end;
      if FindFirst(Path +'*', faAnyfile,sch) = 0 then
      begin
        repeat
          Application.ProcessMessages;
          if ((sch.Name = '.') or (sch.Name = '..')) then  Continue;
          if isSearchfolder and DirectoryExists(Path+sch.Name) then
              Result.Add(sch.Name); 
          if (not isSearchfolder) and ((UpperCase(extractfileext(Path+sch.Name))=UpperCase(FileExt)) or (FileExt='.*')) then
              Result.Add(sch.Name);
        until FindNext(sch) <> 0;
        SysUtils.FindClose(sch);
      end;
    end;调用
    procedure TForm1.Button1Click(Sender: TObject);
    var
      TempPngList:TStringList;
      i:Integer;
    begin
      TempPngList:=TStringList.Create;
      TempPngList:=GetFolderList('E:\','.png'); //路径自己改  第二个参数是后缀名 
      for i:=0 to TempPngList.Count-1 do
        ShowMessage(TempPngList[i]);
    end;
      

  9.   

    代码方式function GetFolderList(Path,FileExt:string):TStringList;
    var
      sch:TSearchrec;
      isSearchfolder:Boolean;
    begin
      if FileExt='\\' then isSearchfolder:=true else isSearchfolder:=false;
      Result:=TStringlist.Create;
      if rightStr(trim(Path), 1) <> '\' then
      Path := trim(Path) + '\'
      else
      Path := trim(Path);
      if not DirectoryExists(Path) then
      begin
      Result.Clear;
      exit;
      end;
      if FindFirst(Path +'*', faAnyfile,sch) = 0 then
      begin
      repeat
      Application.ProcessMessages;
      if ((sch.Name = '.') or (sch.Name = '..')) then Continue;
      if isSearchfolder and DirectoryExists(Path+sch.Name) then
      Result.Add(sch.Name);  
      if (not isSearchfolder) and ((UpperCase(extractfileext(Path+sch.Name))=UpperCase(FileExt)) or (FileExt='.*')) then
      Result.Add(sch.Name);
      until FindNext(sch) <> 0;
      SysUtils.FindClose(sch);
      end;
    end;//调用
    procedure TForm1.Button1Click(Sender: TObject);
    var
      TempPngList:TStringList;
      i:Integer;
    begin
      TempPngList:=TStringList.Create;
      TempPngList:=GetFolderList('E:\','.png'); //路径自己改 第二个参数是后缀名  
      for i:=0 to TempPngList.Count-1 do
      ShowMessage(TempPngList[i]);
    end;