Advanced Files & Folders

Performing file/folder operations

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

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“If debugging is the process of removing software bugs, then programming must be the process of putting them in.” - Edsger Dijkstra