Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 47,280 times

Contents

Related Categories

Advanced Files & Folders - Performing file/folder operations

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

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Re: [38] Advanced Files & Folders

    Posted by unitled on 28 Jul 2006

    Okay, I've used the 'List all files in a directory' code (with a few slight modifications) but whenever I reboot the computer, it stops finding the files that are there (other than that, it works fine...

  • Posted by HyperHacker on 26 Nov 2004

    I think you use the FOF_SILENT flag. Been a while since I did it though.

  • Posted by HyperHacker on 26 Nov 2004

    I think you use the FOF_SILENT flag. Been a while since I did it though.

  • How does one actually Undo?

    Posted by vor0nwe on 22 Sep 2004

    Hi,

    This code snippet works all right. What I’m looking for, however, is how to actually Undo a file operation? I can’t find any way to do that by code...

    Thanks for all tips!

    vor0nwe

  • Minor bugs

    Posted by RobCrombie on 08 Aug 2004

    Hi,
    Couple of small bugs -

    Change Dim sPath As String to strPath

    Change "" to "\" in two lines -
    sStartDir = Left$(sFile, InStrRev(sFile, ""))
    '// just get filename
    ...