Sunday, June 28, 2009

Rename and delete file permanent or send to recycle on vb6

Code following functioning to rename filename and delete file permanently or send to recycle. Make new module, then insert code following:


Option Explicit

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

Enum DeleteFileConst
[Send To Recycle] = 0
[Permanent] = 1
End Enum

Private Const FO_RENAME As Long = &H4
Private Const FOF_ALLOWUNDO = &H40
Private Const FO_DELETE = &H3

Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long

Public Sub Rename(Optional Source As String, Optional Destination As String)
Dim FileOperation As SHFILEOPSTRUCT
Dim lReturn As Long
With FileOperation

.wFunc = FO_RENAME
.pFrom = Source
.pTo = Destination
.fFlags = FOF_ALLOWUNDO

End With

lReturn = SHFileOperation(FileOperation)
End Sub

Public Sub Delete(Optional Source As String, Optional TypeDelete As DeleteFileConst)
Dim FileOperation As SHFILEOPSTRUCT
Dim lReturn As Long

With FileOperation
.wFunc = FO_DELETE
.pFrom = Source
.fFlags = FOF_ALLOWUNDO
End With

Select Case TypeDelete
Case 0: lReturn = SHFileOperation(FileOperation)
Case 1: Kill Source
End Select
End Sub


Example:
  • For rename file
    Rename "C:\test.txt", "C:\test.dat"
    "C:\test.txt" is source file to rename and "C:\test.dat is destination file.

  • For delete file
    Delete "C:\test.txt", 0
    0 for send file to recycle and 1 for delete file permanent.


No comments:

Post a Comment