Deleting Directories

gselliott

Registered User.
Local time
Today, 15:54
Joined
May 17, 2002
Messages
106
I am wanting to automatically delete a Directory when I click a button on my form. I have searched the forum and found the RmDir command but I am unable to get this to work, has anyone any ideas?

Thanks in advance.
 
yes the RmDir is the right way to do it, just place RmDir "pathoffolder" on the onclick event of your button. Note that it does delete your folder without any confirmation. Do a search also for the "Kill" statement !
 
Thanks for the reply, this is the code I have been trying to use:

Dim strDir As String
Dim intDir As Integer

strDir = "c:\Test"
intDir = (fIsFileDIR(strDir, vbDirectory))

If intDir = 0 Then
MsgBox "No file exist so I cant delete it"
Else
MsgBox "File is there and will be deleted"
Kill strDir & "\*.*"
RmDir strDir
End If

With the following Module:

Function fIsFileDIR(stPath As String, Optional lngType As Long) As Integer
On Error Resume Next
fIsFileDIR = Len(Dir(stPath, lngType)) & gt
End Function

This sort of works as it deletes any of the files in the root of the directory but it will not delete any of the sub directories. Is it possible to delete everything in the directory or not?

Thanks again.
 
This will delete the directory and any sub directories within including any files with the directories.
Be warned that this is dangerous stuff if misused!

Code:
Option Compare Database
Option Explicit

Public Type SHFILEOPSTRUCT
    hWnd As Long
    wFunc As Long
    pFrom As String
    pTo As String
    fFlags As Integer
    fAborted As Boolean
    hNameMaps As Long
    sProgress As String
End Type

Public Const FO_DELETE = &H3
Public Const FOF_ALLOWUNDO = &H40
Public Const FOF_SILENT = &H4
Public Const FOF_NOCONFIRMATION = &H10
Public Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long

Public Sub Test()

    Dim sDirectory As String
    sDirectory = "C:\Temp\Testing 1 2 3\XYZ"
    
    DeleteDir (sDirectory)
    MsgBox "The '" & sDirectory & "' Directory was deleted!"

End Sub

Public Sub DeleteDir(sDirectory As String)

    Dim SHFileOp As SHFILEOPSTRUCT

    With SHFileOp
        'Delete the file
        .wFunc = FO_DELETE
        'Select the file
        .pFrom = sDirectory
        'Move to recycle bin, no warning/confirmation messages, no progress meter
        .fFlags = FOF_ALLOWUNDO Or FOF_SILENT Or FOF_NOCONFIRMATION
    End With
    'perform file operation
    SHFileOperation SHFileOp

End Sub
 

Users who are viewing this thread

Back
Top Bottom