To implement a directory-level copy, you can take advantage of the Windows API function SHFileOperation (), whose function is declared as follows: Winshellapi int WINAPI shfileoperation (lpshfileopstruct LP FILEOP); Example: Create a new project with the following program example: Unit Unit1; Interface uses Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls,shellapi; Note: You must refer to the Shellapi cell type TForm1 = Class (Tform) Button1:tbutton; Procedure Button1Click (Sender:tobject); Private {Private declarations} public {public declarations} end; var Form1:tform1; Implementation {$R *.DFM} procedure Tform1.button1click (Sender:tobject); var opstruc:tshfileopstruct; Frombuf,tobuf:array[0..128]of Char; Begin Fillchar (frombuf,sizeof (FROMBUF), 0); Fillchar (Tobuf,sizeof (TOBUF), 0); Assume that all files under the C:\TEMP1 directory are copied to the C:\TEMP2 directory strpcopy (frombuf, ' c:\temp1\*.* '); (\*.*) can be removed strpcopy (tobuf, ' C:\temp2 '); With Opstruc do begin wnd:=handle; Wfunc:=fo_copy; pfrom:[email protected]; pto:[email protected]; Fflags:=fOf_noconfirmation or fof_renameoncollision; Fanyoperationsaborted:=false; Hnamemappings:=nil; Lpszprogresstitle:=nil; End SHFileOperation (OPSTRUC); End End. Through this program, you can copy a subdirectory and all of its following files (including sub-subdirectories) to another subdirectory, if the target directory does not exist, it will be automatically created, thereby actually some of the functions of automatic backup.
Delphi Copy directory (including subdirectories) method