将所有内容从源目录复制到新目录Delphi EX7

时间:2015-03-21 13:30:42

标签: delphi file-copying movefileex

我的最终目标是将所有相关文件从一个文件夹复制到另一个文件夹。所以例如我们有C:\Users\Tool\Desktop\test\oldStuff。在oldStuff文件夹中,我们有更多文件夹以及一些 mp3 mp4 txt 文件。

现在我要做的是将所有小于GB的 mp4 文件复制到C:\Users\Tool\Desktop\test\New_Stuff_Less_than_a_Gig,将 .mp4 文件复制到大于GB C:\Users\Tool\Desktop\test\New_STuff_Bigger_than_a_Gig

我觉得这很容易,但我错了。到目前为止有这个,现在不担心文件类型,所以只需要*.*

 procedure TForm4.Button1Click(Sender: TObject);
 var
   f: TSearchRec;
   Dir: string;
 begin
    if not SelectDirectory(Dir,widestring(Dir),Dir) then   Exit;
    FileMode:=0;
    if FindFirst(Dir+'\*.*',faAnyFile,f) = 0 then
    repeat
         try
          if (f.Attr and faDirectory ) < $00000008 then
          CopyFile(PChar(Dir+'\'+f.Name),PChar
 ('C:\Users\Tool\Desktop\test\new\'+f.Name),false);
         except
          on e: exception do
            ShowMessage(E.Message);
         end;
    until findNext(f) <> 0
 end;

将复制所选文件夹中的任何内容,但不会从所选文件夹中的文件夹中复制任何内容。例如。如果我们有C:\Users\Tool\Desktop\test\oldStuff\movie.mp4,则会复制Movie.mp4文件,但如果我们C:\Users\Tool\Desktop\test\oldStuff\movies\Movie.mp4,则不会复制Movie.mp4文件。我虽然可以做这样的事情

CopyFile.size < 1000 (PChar('C:\Users\Tool\Desktop\test\oldStuff\*.*'+f.Name),
                   PChar('C:\Users\Tool\Desktop\test\new_Stuff\'+f.Name),false)

甚至只是

CopyFile (PChar('C:\Users\Tool\Desktop\test\old\*.*'+f.Name),
                   PChar('C:\Users\Tool\Desktop\test\new\'+f.Name),false);

但它并没有复制任何东西。

1 个答案:

答案 0 :(得分:6)

这是一个例子(在XE7中完成),可以做你想做的事。显然,您需要对其进行修改以满足您的需求;它有硬编码的路径信息和文件掩码(* .png),并使用常量来决定文件是大还是小。

它基于此示例目录树:

D:\TempFiles
  |--\Test
  |-----\A
  |-----\B
  |--------\SubB   
  |-----\NewFiles
  |-------\Large
  L-------\Small

找到 D:\ TempFiles \ Test 及其子文件夹中的所有 .png 文件,并复制等于或大于10KB的文件到 D:\ TempFiles \ NewFiles \ Large 以及小于10KB的 D:\ TempFiles \ NewFiles \ Small

您需要在实施IOUtils条款中添加Typesuses

procedure TForm1.Button1Click(Sender: TObject);
var
  aLargeFiles: TStringDynArray;
  aSmallFiles: TStringDynArray;
const
  LargeSize = 10 * 1024;
  SourcePath = 'D:\TempFiles\Test\';
begin
  aLargeFiles := TDirectory.GetFiles(SourcePath, '*.png',
                   TSearchOption.soAllDirectories,
                   function (const Path: string; const SR: TSearchRec): Boolean
                   begin
                     Result := (SR.Size >= LargeSize);
                   end);
  aSmallFiles := TDirectory.GetFiles(SourcePath, '*.png',
                   TSearchOption.soAllDirectories,
                   function(const Path: string; const SR: TSearchRec):Boolean
                   begin
                     Result := (SR.Size < LargeSize);
                   end);
  CopyFilesToPath(aLargeFiles, 'D:\TempFiles\NewFiles\Large\');
  CopyFilesToPath(aSmallFiles, 'D:\TempFiles\NewFiles\Small\');
end;

procedure TForm1.CopyFilesToPath(aFiles: array of string; DestPath: string);
var
  InFile, OutFile: string;
begin
  for InFile in aFiles do
  begin
    OutFile := TPath.Combine( DestPath, TPath.GetFileName( InFile ) );
    TFile.Copy( InFile, OutFile, True);
  end;
end;