Title: Using the SHFileOperation function
Question: How to copy, move or delete files Windows-like?
Answer:
The answer is simple: Let the Shell do it for you.
With the following function it is an easy job. Remember to add the unit ShellAPI to your uses clause.
Please read the section about SHFileoperation in the Delphi Online Help for possible values of the parameters op and flags.
procedure FileOperation (const source, dest: string;
op, flags: Integer);
var shf: TSHFileOpStruct;
s1, s2: string;
begin
FillChar (shf, SizeOf (shf), #0);
s1:= source + #0#0;
s2:= dest + #0#0;
shf.Wnd:= 0;
shf.wFunc:= op;
shf.pFrom:= PCHAR (s1);
shf.pTo:= PCHAR (s2);
shf.fFlags:= flags;
SHFileOperation (shf);
end;
Some examples of using the function:
Send a file to the trashcan
FileOperation (filename, '', FO_DELETE,
FOF_ALLOWUNDO + FOF_NOCONFIRMATION);
Move a file to another directory
FileOperation (sourcefile, destination, FO_MOVE,
FOF_ALLOWUNDO + FOF_NOCONFIRMATION);
Copy a file to another directory
FileOperation (sourcefile, destination, FO_COPY,
FOF_ALLOWUNDO + FOF_NOCONFIRMATION);