News
Photos
Articles
Components
Applications
Kleinkunst

Delphi - Copy, move, delete and rename files

In the Windows Shell (= Explorer) several Windows API functions are available to copy, move, delete and rename files. Delphi has some older CopyFile, DeleteFile, MkDir … functions but the new shell functions do have several advantages; asking to create folder if it does not exists yet; move to recycle bin instead of deleting, animation with progress bar, …

The API function SHFileOperation does have a lot of parameters so I created some Delphi functions to execute all kind of file operations.

Copying files

// ----------------------------------------------------------------
// Copy files
// ----------------------------------------------------------------
function scShellCopyFile(FormHandle : THandle; StrFrom, StrTo : string; 
  BlnSilent : Boolean = False) : Boolean;
var
  F : TShFileOpStruct;
begin
  F.Wnd:=FormHandle;
  F.wFunc:=FO_COPY;
  F.pFrom:=PChar(StrFrom+#0);
  F.pTo:=PChar(StrTo+#0);
  F.fFlags := FOF_ALLOWUNDO or FOF_RENAMEONCOLLISION;
  if BlnSilent then
    F.fFlags := F.fFlags or FOF_SILENT;
  if ShFileOperation(F) <> 0 then
    result:=False
  else
    result:=True;
end;

Moving files

// ----------------------------------------------------------------
// Move files
// ----------------------------------------------------------------
function scShellMoveFile(FormHandle : THandle; StrFrom, StrTo : string; 
  BlnSilent : Boolean = False) : Boolean;
var
  F : TShFileOpStruct;
begin
  F.Wnd:=FormHandle;
  F.wFunc:=FO_MOVE;
  F.pFrom:=PChar(StrFrom+#0);
  F.pTo:=PChar(StrTo+#0);
  F.fFlags := FOF_ALLOWUNDO or FOF_RENAMEONCOLLISION;
  if BlnSilent then
    F.fFlags := F.fFlags or FOF_SILENT;
  if ShFileOperation(F) <> 0 then
    result:=False
  else
    result:=True;
end;

Deleting files

// ----------------------------------------------------------------
// Delete file or folder
// ----------------------------------------------------------------
function scShellDeleteFile(FormHandle : THandle; StrFile : String; BlnSilent : Boolean = False; 
  BlnConfirmation : Boolean = True; BlnUndo : Boolean = True) : Boolean;
var
  F : TShFileOpStruct;
begin
  F.Wnd:=FormHandle;
  F.wFunc:=FO_DELETE;
  F.pFrom:=PChar(StrFile+#0);
  if BlnUndo then
    F.fFlags := FOF_ALLOWUNDO;
  if not BlnConfirmation then
    F.fFlags := FOF_NOCONFIRMATION;
  if BlnSilent then
    F.fFlags := F.fFlags or FOF_SILENT;
  if ShFileOperation(F) <> 0 then
    result:=False
  else
    result:=True;
end;

Renaming files

// ----------------------------------------------------------------
// Rename files
// ---------------------------------------------------------------- 
function scShellRenameFile(FormHandle : THandle; StrFrom, StrTo : string; <br />  BlnSilent : Boolean = False) : Boolean;
var
  F : TShFileOpStruct;
begin
  F.Wnd:=FormHandle;
  F.wFunc:=FO_RENAME;
  F.pFrom:=PChar(StrFrom+#0);
  F.pTo:=PChar(StrTo+#0);
  F.fFlags := FOF_ALLOWUNDO or FOF_RENAMEONCOLLISION;
  if BlnSilent then
    F.fFlags := F.fFlags or FOF_SILENT;
  if ShFileOperation(F) <> 0 then
    result:=False
  else
    result:=True;
end;