devxlogo

Delete a folder and all its subfolders

Delete a folder and all its subfolders

The RmDir command can delete a directory only if it doesn’t contain files or sub-directories. If the directory you want to delete does contain other files or, worse, subdirectories it seems that you are forced to use a recursive routine that does the job.

A simpler solution is offered by the DeleteFolder method of the FileSystemObject object, exposed by the Microsoft Scripting Runtime library:

' NOTE: this code requires that you add a reference to the'       Microsoft Scripting Runtime type library' delete the C:TEMP directory and all its sub-directoriesDim fso As New Scripting.FileSystemObjectfso.DeleteFolder "c:	emp"

You can also pass a second argument and set it to True if you want to force the deletion of read-only files:

fso.DeleteFolder "c:	emp", True

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist