본문 바로가기

VBA

파일을 휴지통에 넣기(API이용)


파일을 직접 윈도우명령을 이용하여 휴지통에 넣는 방법입니다.
원문은 http://www.cpearson.com 에 있습니다.
링크는 http://www.cpearson.com/excel/recycle.aspx 이니 참조하시기 바랍니다.

전에도 말씀드렸다시피 VB에서는 Kill이라는 명령어를 제공합니다. 이것은 파일을 그대로 지워버립니다. 문제는 휴지통에 보내면서 지우는게 아니라 그냥 지웁니다.
쉬운 방법으로는 지운후 복구할 수 없습니다.
그러나 윈도우 API를 이용하면 파일을 휴지통으로 보낼수 있습니다.
이 명령어와 관련하여 세가지 프로시져를 제공하고 있습니다.

1. 파일을 휴지통으로 보내기(Recycle)
2. 파일을 중요한 파일인지 검사하고 안전하게 휴지통으로 보내기(RecycleSafe)
3. 휴지통 비우기(EmptyRecycleBin)

주의할 점은 파일을 지정할때 드라이브명부터 파일명까지 완전하게 넣어줘야 한다는 것입니다.

C:\Some\Folder\File.xls 이렇게 넣어야 인식하고 처리하게 됩니다.

여기서는 1번 프로시져와 관련된 부분만 붙여놓겠습니다.

공통부분입니다.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Windows API functions, constants,and types.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Declare Function SHFileOperation Lib "shell32.dll" Alias _
    "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long

Private Declare Function PathIsNetworkPath Lib "shlwapi.dll" _
    Alias "PathIsNetworkPathA" ( _
    ByVal pszPath As String) As Long

Private Declare Function GetSystemDirectory Lib "kernel32" _
    Alias "GetSystemDirectoryA" ( _
    ByVal lpBuffer As String, _
    ByVal nSize As Long) As Long

Private Declare Function SHEmptyRecycleBin _
    Lib "shell32" Alias "SHEmptyRecycleBinA" _
    (ByVal hwnd As Long, _
     ByVal pszRootPath As String, _
     ByVal dwFlags As Long) As Long

Private Declare Function PathIsDirectory Lib "shlwapi" (ByVal pszPath As String) As Long

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 Long
    wFunc As Long
    pFrom As String
    pTo As String
    fFlags As Integer
    fAnyOperationsAborted As Boolean
    hNameMappings As Long
    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 varaible.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim SHFileOp As SHFILEOPSTRUCT
Dim Res As Long
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

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
End Function

'VBA' 카테고리의 다른 글

스도쿠 100개 풀기  (0) 2009.08.14
스도쿠 엑셀로 풀기  (3) 2009.08.14
현재열려있는 파일 휴지통에 넣고 지우기  (18) 2009.07.22
전체 쉬트 원복화  (0) 2009.07.21
셀에 체크박스 추가하기  (1) 2009.07.20