[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Complete delete of folder and files using VB6

Posted on 2005-05-09
Medium Priority
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
  • 3
  • 2
  • 2
  • +1
LVL 66

Accepted Solution

Jim Horn earned 200 total points
ID: 13959723
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

Expert Comment

ID: 13961843
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 http://home.kooee.com.au/kingull/xp.html 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 http://aroundcny.com/technofile/texts/bkdeltree98.html

Author Comment

ID: 13961997

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 13962015
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?

Expert Comment

ID: 13962458
Yes, that looks correct.  

Make sure:
1. You downloaded DelTree.zip, 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 66

Expert Comment

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


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

Expert Comment

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


Expert Comment

ID: 13963650
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.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Simple Linear Regression

872 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