在文件夹内搜索大量特定文件名

问题描述 投票:0回答:1

我有一个包含很多文件的文件夹,即:

C:\Tmp\Input\random00001.txt
C:\Tmp\Input\random00002.txt
C:\Tmp\Input\random00003.txt
...
C:\Tmp\Input\random10000.txt

我有一个很大的列表(大约两三百个),我需要将其复制到另一个文件夹,即:

random00002.txt
random00009.txt
random04001.txt
...
random90000.txt

我曾尝试使用Windows搜索使用OR关键字来分隔文件名,但文件太多,它会切断过滤器字符串...

Screenshot of the windows search using the OR keyword

我还试图编写一个函数,该函数允许使用过滤器复制文件,但似乎不适用于OR关键字

uses
  SysUtils, IOUtils;

procedure CopyFiles(const AInputPath : string; const AOutputPath : string; const AFilter : string = '*.*');
var
  Found : boolean;
  Res: TSearchRec;
begin
  Found := (FindFirst(AInputPath + AFilter, faAnyFile, Res) = 0);
  while(Found) do
  begin
    if((Res.Attr AND faDirectory = 0) AND (Res.Name <> '.') AND (Res.Name <> '..')) then
    begin
      TFile.Copy(AInputPath + Res.Name, AOutputPath + Res.Name);
    end;
    Found := (FindNext(Res) = 0);
  end;
  FindClose(Res);
end;
delphi delphi-xe7
1个回答
4
投票

如果您有要复制的名称列表,只需检查当前名称是否属于此列表就可以了>>

if((Res.Attr AND faDirectory = 0) AND (Res.Name <> '.') AND (Res.Name <> '..')) then
    begin
      if  Res.Name in Names then
        TFile.Copy(AInputPath + Res.Name, AOutputPath + Res.Name);
    end;

if Res.Name in Names的实现取决于细节-名称可能是Dictionary,StringList,已排序的一个,依此类推(((您尚未指定细节)]

© www.soinside.com 2019 - 2024. All rights reserved.