Results 1 to 4 of 4

Thread: SHFileOperation

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2000
    Posts
    25

    SHFileOperation

    I'm trying to use SHFileOperation to delete a folder containing many subfolders, but it always says that it can't be deleted b/c the folder's not empty. But I thought that this function could do that... any ideas?

  2. #2
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Try this:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Type SHFILEOPSTRUCT
    4.     hWnd As Long
    5.     wFunc As Long
    6.     pFrom As String
    7.     pTo As String
    8.     fFlags As Integer
    9.     fAborted As Boolean
    10.     hNameMaps As Long
    11.     sProgress As String
    12. End Type
    13.  
    14. Private Const FOF_NOCONFIRMATION = &H10
    15. Private Const FO_DELETE = &H3
    16. Private Const FOF_ALLOWUNDO = &H40
    17. Private Const FOF_FILESONLY = &H80                 '  On *.*, Do only files
    18.  
    19. Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
    20.  
    21. Public Function NuKill(Path As String, Optional Recycle As Boolean = True) As Boolean
    22.  'Nucleus
    23.  'Delete file(s) Or Folder(s) (including subdirectories) And optionally send To recycle bin
    24.  'In: Path of file/folder
    25.  'Out: Boolean indicating success
    26.  
    27.  If Right(Path, 1) = "\" Then Path = Left(Path, Len(Path) - 1)
    28.  If Len(Dir$(Path)) <> 0 Or Len(Dir$(Path, vbDirectory)) <> 0 Then
    29.     Dim SHFileOp As SHFILEOPSTRUCT
    30.     With SHFileOp
    31.         'Delete the file
    32.         .wFunc = FO_DELETE
    33.         'Select the file
    34.         .pFrom = Path
    35.         .fFlags = FOF_NOCONFIRMATION Or FOF_FILESONLY
    36.        
    37.         'next optionally allow move To recycle bin
    38.         'which only works For whole folders Or single file operations
    39.         'it will Not send *.* Or *.txt To recycle bin
    40.         'due To built-in limitations In api Function
    41.         If Recycle Then .fFlags = .fFlags Or FOF_ALLOWUNDO
    42.     End With
    43.     'perform file operation
    44.     NuKill = (SHFileOperation(SHFileOp) = 0)
    45.  End If
    46. End Function

    Usage:
    VB Code:
    1. Private Sub Command1_Click()
    2. 'NuKill "c:\tmp\book1.xls" 'delete Single file And send To recycle bin
    3. MsgBox NuKill("c:\tmp")  'delete folder And send To recycle bin
    4. 'NuKill "c:\temp",false" 'delete folder, don't send To recyle bin
    5.  
    6. 'Next the limitations:
    7. 'Note you can use kill\*.* instead for single directory operations
    8. 'NuKill "c:\tmp\*" ' delete all files In folder can't send To recycle bin due To api Function limitation
    9. 'NuKill "c:\tmp\*.txt" ' delete all text files In folder, can't send To recyle bin due To api Function limitations
    10. End Sub

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2000
    Posts
    25
    Thanks- that works!

  4. #4
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width