VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



DeleteFileEx Function

by Ruturaaj (4 Submissions)
Category: Files/File Controls/Input/Output
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (2 Votes)

This function can do following things ...
[1] Show default system confirmation Prompt to move File to Recycle Bin
[2] Move directly the selected File to Recycle Bin without any Prompt
[3] Show default system confirmation Prompt to remove File forever.
[4] Delete File forever without any prompt. (Same lile Kill function)

Code Returns
Returns Boolean value. True if task completed without any error , or else returns False on error.
API Declarations
Private Declare Function SHFileOperation Lib "shell32.dll" (lpFileOp As SHFILEOPSTRUCT) As Long
Private Const FO_DELETE = &H3
Private Const FOF_ALLOWUNDO = &H40
Private Const FOF_NOCONFIRMATION = &H10
Private 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 String
End Type

Rate DeleteFileEx Function

Private Declare Function SHFileOperation Lib "shell32.dll" (lpFileOp As SHFILEOPSTRUCT) As Long
Private Const FO_DELETE = &H3
Private Const FOF_ALLOWUNDO = &H40
Private Const FOF_NOCONFIRMATION = &H10
Private 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 String
End Type
Public Function DeleteFileEx(lHwnd As Long, sFilePathName As String, bToRecycleBin As Boolean, Optional bConfirm As Boolean = True) As Boolean
'---------------------------------------------------------------------------------------
' Author   : Ruturaj
' Email   : [email protected]
'=======================================================================================
' Procedure : DeleteFileEx
' Type    : Function
' ReturnType : Boolean
'=======================================================================================
' Arguments : [1] lHwnd      = hWnd Property of calling Form.
'       [2] sFilePathName  = Full path of File which is to be deleted.
'       [3] bToRecycleBin  = Set to True if file is to be moved to Recycle Bin.
'       [4] bConfirm     = Optional. Default is True. Set to False if you
'                   don' want OS Confirmation prompts before
'                   performing Delete Action.
'=======================================================================================
' Purpose  : This function can do following things ...
'       [1] Show default system confirmation Prompt to move File to Recycle Bin
'       [2] Move directly the selected File to Recycle Bin without any Prompt
'       [3] Show default system confirmation Prompt to remove File forever.
'       [4] Delete File forever without any prompt. (Same lile Kill function)
'---------------------------------------------------------------------------------------
  On Error GoTo DeleteFileEx_Error
  
  Dim TSHStruct As SHFILEOPSTRUCT
  Dim lResult As Long
  
  'See if File exists ...
  If Dir(sFilePathName, vbHidden Or vbNormal Or vbReadOnly Or vbSystem) <> "" Then
    'Fill the necessary Structure elements by specified values ...
    With TSHStruct
      .hwnd = lHwnd
      .pFrom = sFilePathName
      .wFunc = FO_DELETE
      
      'Flag settings ... the heart of this function !
      If bToRecycleBin = True Then
        If bConfirm = True Then
          .fFlags = FOF_ALLOWUNDO
        Else
          .fFlags = FOF_ALLOWUNDO Or FOF_NOCONFIRMATION
        End If
      ElseIf bToRecycleBin = False Then
        If bConfirm = False Then
          .fFlags = FOF_NOCONFIRMATION
        End If
      End If
    End With
    
    'It's Show-Time !
    lResult = SHFileOperation(TSHStruct)
    
    'SHFileOperation returns Zero if successful or non-zero if failed.
    If lResult > 0 Then
      DeleteFileEx = False
    Else
      DeleteFileEx = True
    End If
  Else
    DeleteFileEx = False
  End If
  'This will avoid empty error window to appear.
  Exit Function
DeleteFileEx_Error:
  'Show the Error Message with Error Number and its Description.
  MsgBox Err.Number & " : " & vbCrLf & vbCrLf & Err.Description, vbInformation, "Error ! (Source : DeleteFileEx)"
  'Safe Exit from DeleteFileEx
  Exit Function
End Function

Download this snippet    Add to My Saved Code

DeleteFileEx Function Comments

No comments have been posted about DeleteFileEx Function. Why not be the first to post a comment about DeleteFileEx Function.

Post your comment

Subject:
Message:
0/1000 characters