План: 1) Получаем список нужных файлов (по дате); 2) Создаем директории (при необходимости) и копируем.
Вот рабочий примерчик (проект прикрепил):
| Код | unit Unit2;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, StdCtrls;
type TForm2 = class(TForm) Edit1: TEdit; Edit2: TEdit; Button1: TButton; DateTimePicker1: TDateTimePicker; ListBox1: TListBox; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;
var Form2: TForm2;
implementation
{$R *.dfm}
procedure SearForFilesWithSpecDate(const curPath: AnsiString; curDate: TDateTime; var filesList: TStringList); var SearchRec: TSearchRec; begin
if FindFirst(curPath + '*.*', faAnyFile, SearchRec) = 0 then begin repeat Application.ProcessMessages; if ((SearchRec.Attr and faDirectory) <> faDirectory) then begin if (FileDateToDateTime(SearchRec.Time) > curDate) then filesList.Add(curPath + SearchRec.Name) end else if (SearchRec.Name <> '..') and (SearchRec.Name <> '.') then begin SearForFilesWithSpecDate (IncludeTrailingPathDelimiter(curPath + SearchRec.Name), curDate, filesList); end; until FindNext(SearchRec) <> 0; FindClose(SearchRec); end; end;
procedure CopyFiles(filesList: TStringList; const dest: AnsiString; const curPathLength: Word); var i: Integer; curDir, curFile: AnsiString; begin for i := 0 to filesList.Count - 1 do begin // Строим новые директории curFile := dest + Copy(filesList[i], curPathLength + 1, Length(filesList[i])); curDir := ExtractFileDir(curFile); if Not FileExists(curDir) then ForceDirectories(curDir); if CopyFile(PChar(filesList[i]), PChar(curFile), False) then Form2.ListBox1.Items[i] := Form2.ListBox1.Items[i] + ' (скопирован)'; end; end;
procedure TForm2.Button1Click(Sender: TObject); var filesList: TStringList; curPath: AnsiString; begin
filesList := TStringList.Create();
Screen.Cursor := crHourGlass; try curPath := IncludeTrailingPathDelimiter(Edit1.Text); SearForFilesWithSpecDate(curPath, DateTimePicker1.Date, filesList); ListBox1.Items.Assign(filesList); CopyFiles(filesList, IncludeTrailingPathDelimiter(Edit2.Text), Length(curPath)); finally Screen.Cursor := crDefault; filesList.Free; end; end;
end.
|
|