?
Solved

recursive deletion

Posted on 2003-11-20
5
Medium Priority
?
388 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 7

Accepted Solution

by:
wsteegmans earned 400 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Suggested Courses
Course of the Month14 days, 2 hours left to enroll

800 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