delphi 列出目录中的文件名并进行排序  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi 列出目录中的文件名并进行排序


示例,假如我在文件夹中有这些文件名:

  

  

File1中

  

文件2

  

文件3

  

FILE4

  

File5

  

File6

  

File7

  

File8

  

File9

  

File10

  

FILE11

  

File12

  

File13

  

FILE14

  

File15

  

File16

  

File17

  

File18

  

File19

  

File20

  

File21

  

File22


function SortFilesByName(List: TStringList; Index1, Index2: Integer): Integer;

var

  FileName1, FileName2: String;

  FileNumber1, FileNumber2: Integer;

begin

  // assuming the files are all named "Path\File###.xxx",

  // where "###" is the number to sort on...

  FileName1 := ChangeFileExt(ExtractFileName(List[Index1]), '');

  FileName2 := ChangeFileExt(ExtractFileName(List[Index1]), '');

  FileNumber1 := StrToInt(Copy(FileName1, 5, MaxInt));

  FileNumber2 := StrToInt(Copy(FileName2, 5, MaxInt));

  Result := (FileNumber2 - FileNumber1);

end;


var 

  SL: TStringList; 

  SR: TSearchRec;   

begin 

  SL := TStringList.Create; 

  try 

    if FindFirst(Path + '*.*', faAnyFile and (not faDirectory) and (not faHidden), SR) = 0 then 

    try

      repeat 

        SL.Add(Path + SR.Name) 

      until FindNext(SR) <> 0; 

    finally

      FindClose(SR); 

    end;


    SL.CustomSort(SortFilesByName);

    // handle the filenames.. 

  finally 

    SL.Free; 

  end; 

end; 



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.034461975097656 seconds