Solved

recursive deletion

Posted on 2003-11-20
5
381 Views
Last Modified: 2010-05-03
I need to create a function that will recurse through a folder structure and delete all the files in them. This will then allow me to delete the folders when empty. I am trying to delete a folder off an ftp site but this can only be done if the folder is empty. I do not know how many subfolders there may be in ths folder or how many files in the folder.

All I know is the current path i am on and the contents of the directories.

I can delete all the files in 1 go but to delete a directory it has to be empty.

e.g i an use the kill *.* command to delete the files and the RMdir command to delete the folders but this has to be empty. I cannot use the fsscipt commands as it is on a remote site.

Thanks in advance if you can help Simon
0
Comment
Question by:scg198
  • 3
5 Comments
 
LVL 7

Accepted Solution

by:
wsteegmans earned 200 total points
ID: 9790196
A real DelTree!

Public Sub DelTree(sFolder As String)
    Dim sCurrFile As String
   
    sCurrFile = Dir(sFolder & "\*.*", vbDirectory)
   
    ' Loop all files/directories ...
    Do While Len(sCurrFile) > 0
       
        ' Skip the directories . and ..
        If sCurrFile <> "." And sCurrFile <> ".." Then
       
            ' If it's a folder, call Deltree Recursively
            If (GetAttr(sFolder & "\" & sCurrFile) And vbDirectory) = _
                vbDirectory Then
               
                Call DelTree(sFolder & "\" & sCurrFile)
               
                ' Next
                sCurrFile = Dir(sFolder & "\*.*", vbDirectory)
            Else
           
                ' It's a file, so delete ...
                Kill sFolder & "\" & sCurrFile
                sCurrFile = Dir
            End If
           
        ' If it's . or .., just next ...
        Else
            sCurrFile = Dir
        End If
       
    Loop
   
    ' Remove the folder itselves!
    RmDir sFolder
   
End Sub
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9790646
Thanks a great function wsteegmans, but scg198 is looking for a recursive delete for a client logged onto an FTP server.

"I am trying to delete a folder off an ftp site ..."

Idle_Mind
0
 
LVL 7

Expert Comment

by:wsteegmans
ID: 9790890
Idle_Mind ,

You dropped your question in the Visual Basic Group ... That's why ...
Could maybe dropped it ... under WebServer or something like that

Anyways ... Did you try to make the connection via your Windows Explorer, or by the Internet Explorer ...
Just type ftp://xxx.xxx.xxx.xxx, connect with login and password, and your directory-structure appears in your Explorer ...

There, just click on a your Folder and Press-Delete ... Everything is deleted: Files and Subfolders ...

Success!
0
 
LVL 7

Expert Comment

by:wsteegmans
ID: 9790900
scg198,

Sorry, took the wrong name ... :-(

The same ;-)

You dropped your question in the Visual Basic Group ... That's why ...
Could maybe dropped it ... under WebServer or something like that

Anyways ... Did you try to make the connection via your Windows Explorer, or by the Internet Explorer ...
Just type ftp://xxx.xxx.xxx.xxx, connect with login and password, and your directory-structure appears in your Explorer ...

There, just click on a your Folder and Press-Delete ... Everything is deleted: Files and Subfolders ...

Success!

0
 

Author Comment

by:scg198
ID: 9795657
Even though you did it for the local drive wsteegmans you answered one question that i had not tried calling the same function over and over again which is the answer so you can have the points
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

708 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

15 Experts available now in Live!

Get 1:1 Help Now