Copy, Move, Delete Files and Folders Using SHFileOperation

Still using Kill, Name, ... to delete, rename, ... files and folders? Why? Do it like Windows does. Its simple to copy, move, rename, delete or send to the Recycle Bin both files and folders. Your applications can display the same dialog boxes, status and confirmation messages and progress indicators (flying files avi) as Windows. You can even use the standard wildcard characters.

Copy, Move, Rename and Delete both files and folders like Windows.
Download Source Code

Description

The SHFileOperation API lets you perform all of the above mentioned operations. All you need to do is populate a SHFILEOPSTRUCT structure, which is shown in the table below, and call SHFileOperation.

The wFunc element of SHFILEOPSTRUCT gets set to the operation you want to perform. Typical values are FO_COPY (Copy), FO_DELETE (Delete), FO_MOVE (Move) and FO_RENAME (Rename). pFrom is a vbNull delimited string of files or folders to perform the operation on. When copying, moving or renaming, pTo is a vbNull delimited string of destination files or folders. Both pFrom and pTo must end with a double null.

The fFlags element serves to customize the operation being performed. Common values are:

  • FOF_SILENT (Show progress indicator)

  • FOF_ALLOWUNDO (Lets you undo the operation)

  • FOF_NOCONFIRMATION (Answer Yes to all questions such as "replace existing file?")

  • FOF_RENAMEONCOLLISION (Renames files to Copy x of x if the file exists)

  • FOF_NOCONFIRMMKDIR (Don't confirm creating a new folder if the operation requires one)

  • FOF_FILESONLY (Perform the operation only on files is wildcards are used)

By combining FO_DELETE and FOF_ALLOWUNDO you can send a file or folder to the Recycle Bin instead of deleting it.

Public Type SHFILEOPSTRUCT

hwnd

As Long

'Handle of dialog box to display status info.

wFunc

As Long

'Operation to perform.

pFrom

As String

'A string specifying one or more source file names.
'Multiple names must be null-separated. The list of names
'must be double null-terminated.

pTo

As String

'Same as pFrom except for the destination.

fFlags

As Integer

'Flags that control the file operation - See Source Code.

fAborted

As Boolean

'TRUE if an operation was aborted before it was completed.

hNameMaps

As Long

'Only used with certain flags.

sProgress

As String

'Title for a progress dialog box.

End Type

Instructions

Enter a valid path to a file or folder in the source textbox. The standard wildcard characters are allowed so that you can copy, move, ... multiple files. Enter a valid path to the destination file or folder. Select the desired option(s) and click the button for the operation to perform.

Experiment with the different options. Copy a folder then try copying it to the same destination again. Move a large folder to see the "files fly by". Rename folders. Then try the same operations with a different set of options selected.


About TheScarms
About TheScarms


Sample code
version info

If you use this code, please mention "www.TheScarms.com"

Email this page


© Copyright 2016 TheScarms
Goto top of page