3

私の最終目標は、関連するすべてのファイルをあるフォルダーから別のフォルダーにコピーすることです。たとえば、 がありC:\Users\Tool\Desktop\test\oldStuffます。フォルダoldStuffには、いくつかのmp3mp4、およびtxtファイルだけでなく、さらに多くのフォルダがあります。

ここで、1 GB より小さいすべてのmp4C:\Users\Tool\Desktop\test\New_Stuff_Less_than_a_Gigファイルを にコピーし、1 GB より大きい.mp4C:\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);

しかし、それは何もコピーしませんでした。

4

1 に答える 1

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 .

実装句にIOUtilsandを追加する必要があります。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;
于 2015-03-21T17:19:01.637 に答える