Complete delete of folder and files using VB6

The kill command will not delete files that are in nested folders (ex: c:\Folder1\SubDir1\FileToDelete.txt
How can I kill an entire set of folders using VB6?
Who is Participating?
Jim HornConnect With a Mentor Microsoft SQL Server Developer, Architect, and AuthorCommented:
Projects:Reference, set a reference to 'Microsoft Scripting Library' (a.k.a. FileSystemObject or fso)

Then run this funciton...

Function fn_delete_directory(ByVal sPath As String) As Boolean

'Delete a directory and all its contents.
'  Returns TRUE if found, FALSE if not found.
'  Returns TRUE if user passed bCreate = True and code created the directory.

On Error Resume Next

Dim fso As FileSystemObject
Set fso = CreateObject("Scripting.FileSystemObject")

Dim fl As Folder
Set fl = fso.GetFolder(sPath)

fl.Delete True

Call fso.DeleteFolder(sPath, True)

Set fso = Nothing

If fn_validate_directory(sPath, False) Then
    fn_delete_directory = True
    fn_delete_directory = False
End If
End Function
There is a DOS command (little known to most new programmers) called DelTree that will do it all in a single command, and gives you some power that isn't normally available to most new programmers.  To use it in VB6 you simply shell out to it.  I just recently downloaded it from but you can do a search on Google and get it from a lot of places -- you just download it and put it in your SYSTEM32 directory.

In VB6 it takes a single statement to delete a folder and all of it's contents, including subfolders and any files, whether they are Read Only or not!  Have you ever had one of those files somehow stuck open that wouldn't let you delete a folder and windows wouldn't let you delet it?  DelTree handles it without a blink.  Here's the simplest way to use it in VB6 code.

  Shell ("deltree /Y c:\folder1")

Don't point it to the wrong place.  I've known a few people who accidentally did a deltree on c:\ in the old days!

You can do some amazing things with DelTree, once you learn it's wild card capability.  To instantly get the inside scoop, I suggest this tiny web site:  The Hidden Power of DelTree at
robbperkAuthor Commented:

once i cut and paste the function into my main form, how do I call it?  sorry, not yet too familiar with how functions work in vb.

i assume there is a simple command that will call the function.

also how do i setup the file location using this function?

is it:

fl = """"""""""

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

robbperkAuthor Commented:
easywriter,  thanks for the info.
unfortunately, it did not do anything at all.

i am storing the file location in a text1 box, so i placed you suggestion into my code as:

shell ("deltree /Y " & text1.text)

is this correct?
Yes, that looks correct.  

Make sure:
1. You downloaded, unzipped it, and put the DelTree.EXE in your system32 directory.
2. Your text1.text box has a valid path in it

To see what is causing the problem, you can open a DOS box and type DELTREE and press enter.  

1. If it says: "'DELTREE' is not recognized as an internal or external command" then it means you don't have the DelTree.EXE put in a place where your program can find it.  I use the C:\Windows\System32 folder, because it is always in the path.

2. If it says: Cannot Load VDM...", etc., then it means you've got the EXE in the right place, but  your VB code that calls the command needs to be tweaked.

What are your results when you do that?
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
SomeYesNoVariable = fn_delete_directory("Your directory goes here")


Call fn_delete_directory("Your directory goes here")
Bob LearnedCommented:
Deltree depends on the operating system.  XP doesn't have the Deltree command.  You would have to test in a Command Window to see the error, if one occurs.

DelTree works on all Windows operating systems, but it does not come built in anymore.  You need to download it, unzip it, and put it in your system32 folder.  It is actually a DOS command.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.