如何將一個目錄下的所有文件名稱抓取放到一個數組中并按名稱自動排序?

解决方案 »

  1.   

    //遍历文件夹下的所有文件,剩下的就存进数据库了,很简单
    //存入完毕后按顺序选出来存入数组
    procedure TForm1.Button1Click(Sender: TObject);
    var f:tsearchrec;
        s:string;
    begin
      if edit1.Text='' then 
         exit;
      if edit1.text[length(edit1.text)]<>'\' then
         s:=edit1.Text+'\*.*'
      else 
         s:=edit1.Text+'*.*';
      findfirst(s,faanyfile,f);
      while  findnext(f)=0 do
         //存进数据库
      //从数据库中取出来,sql语句用order by 书名字段,然后放到数组中
    end;
      

  2.   

    //=====================================================================
    // 函数名称: FindAll
    // 功能描述: 找指定目录下的文件
    // 参    数:  APath   : 路径名称
    //            APropty : 属性名称(*.* | *.txt)
    //            AFiles  : 文件列表
    // 返 回 值:
    // 说    明:
    //=====================================================================
    procedure FindAll(const APath: String; var AFiles: TStringList; APropty: String = '*.XML');
    var
      FPath: String;
      FS: TsearchRec;
    begin
      FPath := IncludeTrailingPathDelimiter(APath);
      AFiles.Sorted := True; // 自动排序
      if FindFirst(FPath + APropty, faAnyFile, FS) = 0 then
      begin
        if (FS.Name <> '.') and (FS.Name <> '..') then
        begin
          if (FS.Attr and faDirectory) = faDirectory then
            FindAll(FPath + FS.Name, AFiles)
          else
            AFiles.Add(StrPas(StrUpper(PChar(FPath))) + StrPas(PChar(FS.Name)));
        end;
        while FindNext(FS) = 0 do
        begin
          if (FS.Name <> '.') and (FS.Name <> '..') then
          begin
            if (FS.Attr and faDirectory) = faDirectory then
              FindAll(FPath + FS.Name, AFiles)
            else
              AFiles.Add(StrPas(StrUpper(PChar(FPath))) + StrPas(PChar(FS.Name)));
          end;
        end;
      end;
      FindClose(FS);
    end;