Complete delete of folder and files using VB6

Posted on 2005-05-09
Last Modified: 2008-03-03
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?
Question by:robbperk
    LVL 65

    Accepted Solution

    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
    LVL 2

    Expert Comment

    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

    Author Comment


    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 = """"""""""


    Author Comment

    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?
    LVL 2

    Expert Comment

    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?
    LVL 65

    Expert Comment

    by:Jim Horn
    SomeYesNoVariable = fn_delete_directory("Your directory goes here")


    Call fn_delete_directory("Your directory goes here")
    LVL 96

    Expert Comment

    by:Bob Learned
    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.

    LVL 2

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Suggested Solutions

    Title # Comments Views Activity
    map interface methods 3 45
    haveThree challenge 22 89
    hasOne  challenge 59 63
    countX 22 49
    This article is filled with multiple code samples and explanations for mathematical calculations. They are as follows: 1. General tips 2. Quadratic formula 3. Object collision 4. Projectile path General Tips       Here are some of my tips f…
    A short article about problems I had with the new location API and permissions in Marshmallow
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now