deleting a non empty directory (1 Viewer)

Dvorak Pietrek

New member
Local time
Today, 02:57
Joined
Sep 6, 2025
Messages
18
hi

hoe to delete a non empty directory using Access VBA 2017?

THANKS
 
delete folder
Code:
Sub DeleteFolderFSO(ByVal FolderPath$)
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    If fso.FolderExists(FolderPath) Then
        fso.DeleteFolder FolderPath, True  ' True = force delete non-empty folder
        MsgBox "Folder deleted successfully.", vbInformation
    Else
        MsgBox "Folder not found.", vbExclamation
    End If
    Set fso = Nothing
End Sub

To use:

DeleteFolderFSO "C:\somefolder"
 
That probably means some app or service has a file open in that folder. Reboot the machine and try again.
 
I am using Access VBA 2007 and is the only app active. If I close it, then the directory releases and I can delete it manually. But I want to delete this directory just from my Access VBA code.
 
I am using Access VBA 2007 and is the only app active. If I close it, then the directory releases and I can delete it manually. But I want to delete this directory just from my Access VBA code.
Are you saying the Access file is also in the directory you want to delete?
 
I am using Access VBA 2007 and is the only app active. If I close it, then the directory releases and I can delete it manually. But I want to delete this directory just from my Access VBA code.
Ii hope it is not the same directory your ACCESS file is in.🫣
 
Have you tried deleting all files in the folder first, then deleting the empty folder? That should tell you if a file is busy.
 
yes i deleted all the files in the directory but can't delete this empty direct
Have you checked the permissions on the d:\pics\docs directory? Is d: a removable drive that has write-protect enabled? Have you tested creating and removing another folder on d: ?
 
You might try adding the file path to your Trusted Locations in ACCESS first and then trying to delete the folder. I can't think of anything else.
 
Things that keep a folder from being deleted:

1. Permissions. The folder itself must have DELETE permission and the folder that contains the folder must allow WRITE permission.
2. Folder is occupied. Windows will not leave an orphaned file, so if the folder contains any files, you must delete the folder contents first.
3. File System lock. If ANY program is reading the file (in this case, the folder), there is a file system lock and you have to terminate the program.
4. Device state. If the device happens to be removable and was dynamically mounted READ-ONLY, you can't delete the file.

Within file system locks, there is this little "gotcha" - if you have Windows Explorer looking at the folder, THAT is a file system lock because Explorer is a program that takes out locks, too. Explorer opens the folder READ/WRITE (if permissions allow it) because you might want to manually copy or delete files therein. This also applies if you have a network session looking at or into the folder - such as a split DB with the FE on one PC looking at the shared BE on another machine being or acting as a file server.

Within permissions, besides the requirement for being allowed to delete the files, there is the matter (as noted by @LarryE) of certain areas being locked against you via Discretionary Access Control settings, including but not limited to Trusted Locations. If you have child files in the folder, then you need the child files to ALSO allow DELETE access rights and the folder directly in question must allow you WRITE permissions.

While Windows isn't perfect, it can be perfectly obstinate about not deleting busy files. The file system depends on self-consistency, which would be violated if it allowed you to delete a folder without first deleting its contained files. Those files would get "lost" and the only way to find them again would be a full-blown DiskScan that would put them in a file recovery folder.
 
copy all the Modules on the sample db to your db.
you can call:

Code:
ForceDeleteFolder "D:\YourFolderName"

it will "force close" any open files then proceed deleting the folder.
 

Attachments

Last edited:

Users who are viewing this thread

Back
Top Bottom