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.
Download Source Code
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
|
|
wFunc
|
As Long
|
|
pFrom
|
As String
|
|
pTo
|
As String
|
|
fFlags
|
As Integer
|
|
fAborted
|
As Boolean
|
|
hNameMaps
|
As Long
|
|
sProgress
|
As String
|
|
End Type
|
|
|
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.
|