Recursive file delete keeping folder structure

P

Paul

I'm trying to make get my app to delete all the files in a specified folder
and all the files within the folders of the specified folder.

e.g.
Folder 1 contains three files (File1, File2, File3) and two folders
(Subfolder 1, Subfolder 2).
.......I need to delete File1, File2, File3.
Subfolder 1 contains FileA.
.......Need to delete FileA.
Subfolder 2 contains FileB, FileC, FileD.
.......Need to del FileB, FileC, FileD.

So, basically all files but keeping the folders, so I'll have an empty
folder structure. The code below doesn't seem to work for me. I have admin
rights so file/folder privileges not applicable here.

Dim ParentFolder As New System.IO.DirectoryInfo("C:\TEST\")
Dim Folder As System.IO.DirectoryInfo
Dim File As System.IO.FileInfo
For Each Folder In ParentFolder.GetDirectories()
For Each File In Folder.GetFiles
File.Delete()
Next File
Next Folder


Any ideas?
Cheers,
Paul
 
P

Paul

Ok, turns out it was deleting the files in the sub folders but not the the
files in the parent folder. Proper working code is below for anyone who
wants to know.

Paul


Dim ParentFolder As New System.IO.DirectoryInfo("C:\TEST\")
Dim Folder As System.IO.DirectoryInfo
Dim File As System.IO.FileInfo

For Each Folder In ParentFolder.GetDirectories()
For Each File In ParentFolder.GetFiles
File.Delete()
Next

For Each File In Folder.GetFiles
File.Delete()
Next File
Next Folder
 
G

Guest

Private Sub DeleteAllFiles(ByVal sPath As String)
For Each strFile As String In IO.Directory.GetFiles(sPath)
IO.File.Delete(strFile)
Next
For Each strDir As String In IO.Directory.GetDirectories(sPath)
DeleteAllFiles(strDir)
Next
End Sub
 
P

Paul

Turns out the previous code didn't delete files if their was no subfolders
within the parent folder so I moved a For Each Next around and that fixed
it.
Here is the fixed code with a couple of improvements. I'm no VB expert so
please don't flame me if my code is poor!


'Presumes textbox contains valid path
Dim ParentFolder As New System.IO.DirectoryInfo(txtPath.Text)
Dim Folder As System.IO.DirectoryInfo
Dim File As System.IO.FileInfo
Dim FileCount As Integer

If ParentFolder.Exists = True Then
For Each File In ParentFolder.GetFiles
File.Delete()
Console.Write(File.FullName & vbCrLf)
FileCount += 1
Next File
For Each Folder In ParentFolder.GetDirectories()
For Each File In Folder.GetFiles
File.Delete()
Console.Write(File.FullName & vbCrLf)
FileCount += 1
Next File
Next Folder
MsgBox("Done! " & FileCount & " file(s) deleted.")
Else
MsgBox("The specified folder does not exist. Please select a
valid parent folder.", _
MsgBoxStyle.Information, "Error")
End If



Paul
 
C

Crouchie1998

If you check out my first post, if has the solution & I have attached a
simple zipped project with file/folder structure too.

Download it & try it or just see the deletion routine in the actual post
 
O

OpticTygre

Just out of curiousity, is something like this dangerous to use? I know it
sounds improbable, but if I had a directory structure with subdirectories
1,000 layers deep, then realistically, this would call itself 1,000 times.
I know that won't normally happen in this case, but I've seen this
programming structure used before, and I hear it's a memory hog, if it calls
itself constantly. Is that true?
 
M

Michael D. Ober

See the code change below. You weren't removing the folder itself.

Mike.


Paul said:
I'm trying to make get my app to delete all the files in a specified folder
and all the files within the folders of the specified folder.

e.g.
Folder 1 contains three files (File1, File2, File3) and two folders
(Subfolder 1, Subfolder 2).
......I need to delete File1, File2, File3.
Subfolder 1 contains FileA.
......Need to delete FileA.
Subfolder 2 contains FileB, FileC, FileD.
......Need to del FileB, FileC, FileD.

So, basically all files but keeping the folders, so I'll have an empty
folder structure. The code below doesn't seem to work for me. I have admin
rights so file/folder privileges not applicable here.

Dim ParentFolder As New System.IO.DirectoryInfo("C:\TEST\")
Dim Folder As System.IO.DirectoryInfo
Dim File As System.IO.FileInfo
For Each Folder In ParentFolder.GetDirectories()
For Each File In Folder.GetFiles
File.Delete()
Next File

Folder.Delete ' or Folder.Remove, I don't know which without looking.
 
P

Paul

Thanks Crouchie I just checked your post now. I was about to repost saying I
found that my code didn't recurse through more than one level which yours of
course does.
Cheers for that!

Are you sure you attached a zip file though? Nothing shows up in the post.
Maybe your ISP or virus checker doesn't allow them?
Paul
 
P

Paul

I don't want to delete any folders.
Paul


Michael D. Ober said:
See the code change below. You weren't removing the folder itself.

Mike.




Folder.Delete ' or Folder.Remove, I don't know which without looking.
 
J

Jay B. Harlow [MVP - Outlook]

OpticTygre,
Just out of curiousity, is something like this dangerous to use?
Yes recursion, like any powerful tool, is dangerous.

Recursion is when a routine calls itself. Bounded recursion is recursion
that has a definite condition that stops it. Unbounded recursion is
recursion that continues indefinitely.

Most, if not all, recursive routines can be converted into a non-recursive
routine. The System.Collection.Stack & System.Collection.Queue classes are
useful in this regard. For Example:

Private Sub DeleteAllFiles(ByVal sPath As String)
Dim directories As New Queue
directories.Enqueue(sPath)
Do While directories.Count > 0
sPath = DirectCast(directories.Dequeue(), String)
For Each strFile As String In IO.Directory.GetFiles(sPath)
IO.File.Delete(strFile)
Next
For Each strDir As String In IO.Directory.GetDirectories(sPath)
directories.Enqueue(strDir)
Next
Loop
End Sub

Notice rather then recursion that I use a Queue to maintain the list of
directories that need to be processed. I start the queue with the initial
directory, then simply add directories in each directory to the end of the
queue...

Also notice how the original version might be easier to understand then the
above version...

but if I had a directory structure with subdirectories 1,000 layers deep,
then realistically, this would call itself 1,000 times. Correct.


but I've seen this programming structure used before, and I hear it's a
memory hog, if it calls itself constantly. Is that true?
Each time you call a routine you use space on the stack, if a routine calls
itself "unbounded", then you will receive a stack overflow (a
StackOverflowException). Even a routine that calls itself "bounded" runs the
risk of a stack overflow, however normally the "bound" and the size of the
stack prevents this.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top