Solved

recursive deletion

Posted on 2003-11-20
5
385 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 86

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS Access - Capture pressed key onclick 4 43
passing parameter in sql procedure 9 68
using web browser with BING 40 136
MS SQL store procedure to calculate and return result 6 69
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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

730 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