1

Как можно скопировать все файлы из одной папки в другую папку?

3 ответа 3

3

1) Получаем все файлы из заданной директории, используя комбинацию findfirst->findnext->findclose:

procedure copypast(dir:string; to_dir:string);
var fn:TSearchRec;
begin
dir:=includetrailingbackslash(dir);
to_dir:=includetrailingbackslash(to_dir);
if findfirst(dir+'*',faanyfile,fn)<>0 then exit;  // * - любые файлы 
copyfile(Pchar(dir+fn.name),Pchar(to_dir+fn.name),true);
while findnext(fn)=0  do
copyfile(Pchar(dir+fn.name),Pchar(to_dir+fn.name),true);
findclose(fn);
end;
2

Средствами WinAPI: смотрите раздел File Management Functions. Там есть полный набор функций, CopyFile, FindFirstFile, FindNextFile и т.д.

2

А так пойдёт? Код модуля:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, FileCtrl, Buttons, Gauges;

type
  TForm1 = class(TForm)
    BitBtn1: TBitBtn;
    GroupBox1: TGroupBox;
    DirectoryListBox1: TDirectoryListBox;
    FileListBox1: TFileListBox;
    DriveComboBox1: TDriveComboBox;
    GroupBox2: TGroupBox;
    DirectoryListBox2: TDirectoryListBox;
    FileListBox2: TFileListBox;
    DriveComboBox2: TDriveComboBox;
    GaugeFile: TGauge;
    GaugeAll: TGauge;
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);
Var
  F1, F2: File Of Char;
  Buf: Array[1..2048] Of Char;
  SizeFile, SizeRead: Int64;
  ColRead, ColWrite: Integer;
  i, n: Word;
  FName: String;
begin
  N:=FileListBox1.Items.Count-1;
  GaugeAll.MaxValue:=N+1;

  {$I-}

  For i:=0 To N Do
    Begin
      FName:=DirectoryListBox1.Directory+'\'+FileListBox1.Items.Strings[i];
      AssignFile(F1, FName);
      AssignFile(F2, DirectoryListBox2.Directory+'\'+FileListBox1.Items.Strings[i]);
      Try
        ReSet(F1);
        SizeFile:=FileSize(F1);
        ReWrite(F2);

        ColRead:=0;
        ColWrite:=0;
        SizeRead:=0;
        Screen.Cursor:=crHourGlass;
        While (ColRead=ColWrite) Do
          Begin
            BlockRead(F1, Buf, SizeOf(Buf), ColRead);
            If (ColRead=0) Then Break;
            BlockWrite(F2, Buf, ColRead, ColWrite);
            SizeRead:=SizeRead+ColRead;
            GaugeFile.Progress:=Round(100*SizeRead/SizeFile);
            Application.ProcessMessages;
          End;
        GaugeAll.Progress:=GaugeAll.Progress+1;
        Screen.Cursor:=crDefault;
      Finally
        CloseFile(F1);
        CloseFile(F2);
      End;
    End;

  {$I+}
end;

end.

Интерфейс программы: alt text

3
  • Большое спасибо!!! Правда я хотел через компонент OpenDialog или как там называется?!! 19 фев 2012 в 7:34
  • Ох...жесть =) И вы для этого целую программу писали?
    – AseN
    19 фев 2012 в 9:08
  • Нууу.... не только для этого!!! 19 фев 2012 в 9:10

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.