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

Stu*_*com 3 delphi movefileex file-copying

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

现在,我希望做的是复制所有的MP4文件,这些文件比GB较小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;
Run Code Online (Sandbox Code Playgroud)

它将复制所选文件夹中的任何内容,但不会从所选文件夹中的文件夹中复制任何内容.例如,如果我们有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)
Run Code Online (Sandbox Code Playgroud)

甚至只是

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

但它没有复制任何东西.

Ken*_*ite 6

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

它基于此示例目录树:

D:\TempFiles
  |--\Test
  |-----\A
  |-----\B
  |--------\SubB   
  |-----\NewFiles
  |-------\Large
  L-------\Small
Run Code Online (Sandbox Code Playgroud)

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

您需要添加IOUtilsTypes实现uses子句.

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;
Run Code Online (Sandbox Code Playgroud)

  • 使用错误的变量只是一个小错误.我刚刚纠正了这一点,并改变了所有使用IOUtils的工具 (3认同)