By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,631 Members | 892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,631 IT Pros & Developers. It's quick & easy.

Recursive file delete keeping folder structure

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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


"Paul" <pr*****@private.com> wrote in message
news:Fu********************@pipex.net...
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

Nov 21 '05 #2

P: n/a
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
Nov 21 '05 #3

P: n/a
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
"Paul" <pr*****@private.com> wrote in message
news:7u********************@pipex.net...
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


"Paul" <pr*****@private.com> wrote in message
news:Fu********************@pipex.net...
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


Nov 21 '05 #4

P: n/a
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?
"Crouchie1998" <Cr**********@discussions.microsoft.com> wrote in message
news:11**********************************@microsof t.com...
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

Nov 21 '05 #5

P: n/a
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
Nov 21 '05 #6

P: n/a
See the code change below. You weren't removing the folder itself.

Mike.
"Paul" <pr*****@private.com> wrote in message
news:Fu********************@pipex.net...
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.
Next Folder
Any ideas?
Cheers,
Paul


Nov 21 '05 #7

P: n/a
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


"Crouchie1998" <cr**********@discussions.microsoft.com> wrote in message
news:uV**************@TK2MSFTNGP09.phx.gbl...
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

Nov 21 '05 #8

P: n/a
I don't want to delete any folders.
Paul
"Michael D. Ober" <obermd.@.alum.mit.edu.nospam> wrote in message
news:K4****************@newsread1.news.pas.earthli nk.net...
See the code change below. You weren't removing the folder itself.

Mike.
"Paul" <pr*****@private.com> wrote in message
news:Fu********************@pipex.net...
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.
Next Folder
Any ideas?
Cheers,
Paul


Nov 21 '05 #9

P: n/a
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
"OpticTygre" <op********@adelphia.net> wrote in message
news:xd********************@adelphia.com... 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?
"Crouchie1998" <Cr**********@discussions.microsoft.com> wrote in message
news:11**********************************@microsof t.com...
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


Nov 21 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.