Using Windows API you can perform the following operations:
Copy
Rename
Move
Send to Recycle Bin
Delete permanently
Create Directories
You can also perform any of the operations above with no dialog box. To perform these operations, you need to add a declaration for the SHFileOperation function, and a few constants:
Public Type SHFILEOPSTRUCT
hWnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAnyOperationsAborted As Long
hNameMappings As Long
lpszProgressTitle As Long ' only used if FOF_SIMPLEPROGRESS, sets
dialog title
End Type
Public Declare Function SHFileOperation Lib "shell32.dll" Alias
"SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
' Available Operations
Public Const FO_COPY = &H2 ' Copy File/Folder
Public Const FO_DELETE = &H3 ' Delete File/Folder
Public Const FO_MOVE = &H1 ' Move File/Folder
Public Const FO_RENAME = &H4 ' Rename File/Folder
' Flags
Public Const FOF_ALLOWUNDO = &H40 ' Allow to undo rename, delete ie sends to recycle
bin
Public Const FOF_FILESONLY = &H80 ' Only allow files
Public Const FOF_NOCONFIRMATION = &H10 ' No File Delete or Overwrite
Confirmation Dialog
Public Const FOF_SILENT = &H4 ' No copy/move dialog
Public Const FOF_SIMPLEPROGRESS = &H100 ' Does not display file names
To perform any file functions, you generally use the following syntax:
Dim op As SHFILEOPSTRUCT
With op
.wFunc = Function
.pTo = New Path/Name
.pFrom = Old Path/Name
.fFlags = Flags, seperated by a '+'
End With
' Perform operation
SHFileOperation op
You do not need to fill all paramters for some functions. For example if you are deleting a file, you do not need to set the pTo parameter. So, to delete C: emp.txt to the recycle bin, with no confirmation you use this code:
Dim op As SHFILEOPSTRUCT
With op
.wFunc = FO_DELETE ' Set function
.pFrom = "C:\temp.txt" ' Set File to delete
.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMATION ' Set Flags
End With
' Perform operation
SHFileOperation op
And, to copy the folder C:\backup to C:\backup2, with a simple progress, you would use this code:
Dim op As SHFILEOPSTRUCT
With op
.wFunc = FO_COPY ' Set function
.pTo = "C:\backup2" ' Set new path
.pFrom = "C:\backup" ' Set current path
.fFlags = FOF_SIMPLEPROGRESS
End With
' Perform operation
SHFileOperation op
Comments