获取一个文件夹下的所有文件//不包括文件夹里面的文件// 注意,path后面不要有要有'\';// Memo1.Lines := Searchfile('C:\Users\Admin\Desktop\名人格言\Win32\Release\Data');function Searchfile(path: string): TStringList;varSearchRec: TSearchRec;found: integer;beginResult := TStringList.Create;found := FindFirst(path + '\' + '*.*', faAnyFile, SearchRec);while found = 0 dobeginif (SearchRec.Name <> '.') and (SearchRec.Name <> '..') and(SearchRec.Attr <> faDirectory) thenResult.Add(SearchRec.Name);found := FindNext(SearchRec);end;FindClose(SearchRec);end;
获取一个文件夹下的所有文件//包括文件夹里面的文件uses Masks;
// procedure TForm1.Button1Click(Sender: TObject);// begin// Memo1.Lines.Clear;// GetFileListEx('Z:\', '*.*', Memo1.Lines, False);// Caption:=IntToStr(Memo1.Lines.count);// end;//// procedure TForm1.Button2Click(Sender: TObject);// begin//// Memo1.Lines.Clear;// GetFileListEx('Z:\', '*.cs', Memo1.Lines, true);/// /GetFileListEx('Z:\', '*.cs;*.txt', Memo1.Lines, true);// Caption:=IntToStr(Memo1.Lines.count);// end;
// 遍历目录及子目录procedure GetFileListEx(FilePath, ExtMask: string; FileList: TStrings;SubDirectory: Boolean = True);function Match(FileName: string; MaskList: TStrings): Boolean;vari: integer;beginResult := False;for i := 0 to MaskList.Count - 1 dobeginif MatchesMask(FileName, MaskList[i]) thenbeginResult := True;break;end;end;end;
varFileRec: TSearchRec;MaskList: TStringList;beginif DirectoryExists(FilePath) thenbeginif FilePath[Length(FilePath)] <> '\' thenFilePath := FilePath + '\';if FindFirst(FilePath + '*.*', faAnyFile, FileRec) = 0 thenbeginMaskList := TStringList.Create;tryExtractStrings([';'], [], PChar(ExtMask), MaskList);FileList.BeginUpdate;repeatif ((FileRec.Attr and faDirectory) <> 0) and SubDirectory thenbeginif (FileRec.Name <> '.') and (FileRec.Name <> '..') thenGetFileListEx(FilePath + FileRec.Name + '\', ExtMask, FileList);endelsebeginif Match(FilePath + FileRec.Name, MaskList) thenFileList.Add( { FilePath + } FileRec.Name);end;until FindNext(FileRec) <> 0;FileList.EndUpdate;finallyMaskList.Free;end;end;FindClose(FileRec);end;end;
获取一个文件夹下的所有文件
最新推荐文章于 2023-04-01 17:43:55 发布