Delete Directory

mr_e_landis

Registered User.
Local time
Today, 09:08
Joined
Feb 16, 2005
Messages
28
I am trying to delete a directory using a form.
I currently use the following code.
Code:
Kill Dir1
RmDir Dir2
lets just say I have a series of directories in X:\data\ "dir1", "dir2", "etc."

I think I've just discovered that if I create extra folders within my created directories for example X:\data\dir1\newfolder it will tank.

I've set up error code to prevent a lot of errors from happening but yet here is another error. Its seams like there should be an easier way.

Does anyone know of a quick and easy way to delete a directory with all of its contents?

I don't think this would work but something as simple as this is what I'm looking for... I use Name Dir1 as Dir2 now to move files from one dir to another but use ton's of code to delete...
Code:
I don't think this works!
Name Dir1 as Null
 
From VBA Help:
An error occurs if you try to use RmDir on a directory or folder containing files. Use the Kill statement to delete all files before attempting to remove a directory or folder.
 
The Kill statement will not remove directories if they exist. So if I want to delete X:\data\dir1 and there are folders inside dir1 Kill will not remove them and it will error during the RmDir due to the subdirectories. Is there some way to force a delete?

I'm trying to figure out how to use the following method but can't get it to work.
Code:
object.DeleteFolder folderspec[, force]
can anyone give an example of how to make this work? I think I'm getting hung up on the object...
 
Last edited:
This is my exact code currently...
Code:
    If responce = vbNo Then
        Exit Sub
      Else
        DoCmd.SetWarnings False

        Nuke1 = Me.DataLocation & "\" & [Forms]![fMain]![qTracking Data]![TID No] & "\*."
        Nuke2 = Me.DataLocation & "\" & [Forms]![fMain]![qTracking Data]![TID No]


        Kill Nuke1
        RmDir Nuke2

        DoCmd.OpenQuery "qDELETE TID"
        [Forms]![fMain]![qTracking Data].Requery
        
    End If
 
Try:
Code:
If responce = vbNo Then
    Exit Sub
Else
    DoCmd.SetWarnings False

    Nuke1 = Me.DataLocation & _
                "\" & [Forms]![fMain]![qTracking Data]![TID No] & "\*.[B][COLOR="Red"]*[/COLOR][/B]"
    Nuke2 = Me.DataLocation & "\" & [Forms]![fMain]![qTracking Data]![TID No]


    Kill Nuke1
    RmDir Nuke2

    DoCmd.OpenQuery "qDELETE TID"
    [Forms]![fMain]![qTracking Data].Requery
        
End If
 
Last edited:
I saw that you had an extra * in your code. I figured it would be something that simple but it doesn't make a difference; the subdirectories and the directory I want to delete still exist... :(
 
If the (Me.DataLocation & "\" & [Forms]![fMain]![qTracking Data]![TID No]) directiry has directories in it then you will have to go deeper into the directory structure and delete files and then directories.
 
Object.DeleteFolder

PROBLEM SOLVED!!!
Code:
        Nuke2 = Me.DataLocation & "\" & [Forms]![fMain]![qTracking Data]![TID No]
        
        Dim MyObject As Object
        Set MyObject = CreateObject("Scripting.FileSystemObject")
        MyObject.DeleteFolder Nuke2, True
This will delete the directory and any subdirectories and files within even if they are read only. And there is very little to any error handling needed.

I'm done for the day... I'm going home...:cool:



.
 
Last edited:
Great news! Thanks for posting back with your solution.
 
I tried adding this code to my database, but am still getting the error message that I don't have permissions (which I do). Below is my code. What am I doing wrong?

Dim MyObject As Object
Set MyObject = CreateObject("Scripting.FileSystemObject")
MyObject.DeleteFolder "C:\Report_Data\Log Parser " & Month(Me.FileDate) & "-" & Day(Me.FileDate), True
 
If you have the possability to run a Windows command line, then:

Code:
RD /S/Q "dirnametokill"
would work on Windows NT class OS's.

For the Win 9x flavor, you would need
Code:
DelTree /Y dirnametokill
I seem to recall...
 

Users who are viewing this thread

Back
Top Bottom