The FOF_CREATEPROGRESSDLG is optional, but I like it. It pops up the message box asking if you want to send the file to the recycle bin, or if you are sure you want to delete it.VB Code:
Private Type SHFILEOPSTRUCT hWnd As Long wFunc As Long pFrom As String pTo As String fFlags As Integer fAnyOperationsAborted As Boolean hNameMappings As Long lpszProgressTitle As String End Type Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long Private Const FO_DELETE = &H3 Private Const FOF_ALLOWUNDO = &H40 Private Const FOF_CREATEPROGRESSDLG As Long = &H0 Private Enum WhatDoIDoWithIt SendToRecycle = 1 WhackIt = 2 End Enum Private Function TrashFile(FileName As String, How As WhatDoIDoWithIt) As Boolean Dim FileOperation As SHFILEOPSTRUCT Dim RetCode As Long On Error GoTo WhackItError With FileOperation .wFunc = FO_DELETE .pFrom = FileName If How = SendToRecycle Then .fFlags = FOF_ALLOWUNDO + FOF_CREATEPROGRESSDLG Else .fFlags = FO_DELETE + FOF_CREATEPROGRESSDLG End If End With RetCode = SHFileOperation(FileOperation) If RetCode <> 0 Then TrashFile = False Else TrashFile = True End If Exit Function WhackItError: TrashFile = False MsgBox Err & " " & Error End Function Private Sub Command1_Click() 'to delete it entirely, as with Kill TrashFile "c:\baby.txt", WhackIt 'or to send it to the recycle bin TrashFile "c:\baby.txt", SendToRecycle End Sub




Reply With Quote