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
Solved

recursive deletion

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

856 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