Anh chị cho em hỏi cách xóa thư mục, file. Thư mục, file xóa đi vẫn còn trong thùng rác không bị xóa vĩnh viễn.

Chia sẻ bởi:hands
★★★★★
Quảng cáo

em cám ơn anh chị!

Em xử dụng

Dim fso As Object, FolderToDelete As String
Set fso = CreateObject("Scripting.FileSystemObject")
FolderToDelete= "D:Sample"
If fso.FolderExists(NewFolder) then
       fso.DeleteFolder(FolderToDelete)
End If
hoặc
Dim fso As Object, FileToDelete As String
Set fso = CreateObject("Scripting.FileSystemObject")
FileToDelete= "D:SampleItem1Baitap.xls"
If fso.FileExists(FileToDelete) Then
    fso.DeleteFile(FileToDelete)
End If

Thì Thư mục và file bị xóa vĩnh viễn; không có trong thùng rác nên không lấy lại được khi lỡ xóa nhầm.
em cám ơn anh chị!

Bạn tìm hiểu hàm API của Windows nhé: SHFileOperationW()
Vậy thì dùng lệnh move thay vì xóa, đích là Recycle Bin

7402
em xử dụng đoạn mã theo hướng dẫn trên mạng thì bị lỗi trên. Chỉ cho em cách khắc phục ạ. em dùng 64bit
Đoạn mã em lấy từ:
vbaa2z.blogspot.com/2021/02/vba-to-recycle-file-or-folder.html

bạn cut 3 dòng đầu tiên của code kia, rồi dúng xuống phía dưới cùng xem

'-------------------------------
'Sumitted by: @Husky Passion
'-------------------------------

Option Explicit

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Windows API functions, constants,and types.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Declare PtrSafe Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As LongPtr

Private Const FO_DELETE = &H3
Private Const FOF_ALLOWUNDO = &H40
Private Const FOF_NOCONFIRMATION = &H10
'Private Const MAX_PATH As Long = 260

Private Type SHFILEOPSTRUCT
    hWnd As LongPtr
    wFunc As LongPtr
    pFrom As String
    pTo As String
    fFlags As Integer
    fAnyOperationsAborted As Boolean
    hNameMappings As LongPtr
    lpszProgressTitle As String
End Type

Public Function Recycle(FileSpec As String, Optional ErrText As String) As Boolean
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Recycle
' This function sends FileSpec to the Recycle Bin. There
' are no restriction on what can be recycled. FileSpec
' must be a fully qualified folder or file name on the
' local machine.
' The function returns True if successful or False if
' an error occurs. If an error occurs, the reason for the
' error is placed in the ErrText variable.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim SHFileOp As SHFILEOPSTRUCT
Dim Res As LongPtr
Dim sFileSpec As String

ErrText = vbNullString
sFileSpec = FileSpec

If InStr(1, FileSpec, ":", vbBinaryCompare) = 0 Then
    ''''''''''''''''''''''''''''''''''''''
    ' Not a fully qualified name. Get out.
    ''''''''''''''''''''''''''''''''''''''
    ErrText = "'" & FileSpec & "' is not a fully qualified name on the local machine"
    Recycle = False
    Exit Function
End If

If Dir(FileSpec, vbDirectory) = vbNullString Then
    ErrText = "'" & FileSpec & "' does not exist"
    Recycle = False
    Exit Function
End If

''''''''''''''''''''''''''''''''''''
' Remove trailing '' if required.
''''''''''''''''''''''''''''''''''''
If Right(sFileSpec, 1) = "" Then
    sFileSpec = Left(sFileSpec, Len(sFileSpec) - 1)
End If

On Error Resume Next
With SHFileOp
    .wFunc = FO_DELETE
    .pFrom = sFileSpec
    '.fFlags = FOF_ALLOWUNDO
    '''''''''''''''''''''''''''''''''
    ' If you want to supress the
    ' "Are you sure?" message, use
    ' the following:
    '''''''''''''''''''''''''''''''
    .fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMATION
End With

Res = SHFileOperation(SHFileOp)
If Res = 0 Then
    Recycle = True
Else
    Recycle = False
End If
Err.Clear: On Error GoTo 0
End Function
Sub Test_FileSpec()
   Recycle "C:somepathfile.ext"
End Sub
Option Explicit

Sub test1()

Kill "C:UsersLPDesktopdemofile.txt"

End Sub

Sub test2()

Kill "C:UsersLPDesktopdemoNew Microsoft Excel Worksheet.xlsx"

End Sub

Sub test3()

Dim fsObj As New FileSystemObject
Dim strPath As String

strPath = "C:UsersLPDesktopdemosub1"

'If fsObj.FolderExists(strPath) = True Then
'   fsObj.DeleteFolder strPath
'End If
'
'
'fsObj.DeleteFile strPath & "*.txt", True
'fsObj.DeleteFolder strPath & "*.*", True

End Sub

www.giaiphapexcel.com/diendan/threads/anh-ch%E1%BB%8B-cho-em-h%E1%BB%8Fi-c%C3%A1ch-x%C3%B3a-th%C6%B0-m%E1%BB%A5c-file-th%C6%B0-m%E1%BB%A5c-file-x%C3%B3a-%C4%91i-v%E1%BA%ABn-c%C3%B2n-trong-th%C3%B9ng-r%C3%A1c-kh%C3%B4ng-b%E1%BB%8B-x%C3%B3a-v%C4%A9nh-vi%E1%BB%85n.166338/

Khóa học Power PI – Ứng dung trong Nhân sự
Khóa học SprinGO phù hợp

Khóa học Power PI – Ứng dung trong Nhân sự

TỔNG QUAN KHÓA HỌC: POWER BI CHO NGÀNH NHÂN SỰ Khóa học Power BI cho Nhân sự được thiết kế dành riêng cho các...

Xem khóa học
★★★★★ 5 ★ 1 👤 0 ▥ 0
Quảng cáo

Bạn nên đọc

Bình luận

Quảng cáo

Cũ vẫn chất

Xem thêm