472,807 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,807 software developers and data experts.

Recursive file delete keeping folder structure

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
9 8232
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: LoserInYourFaceEngineer | last post by:
Hello All: I'm having trouble with a recursive function. The function is supposed to identify nested folders in a hierarchical folder structure. The function "searchForFolders()" is...
1
by: wurlds_wurst_coder | last post by:
I want to create a directory structure and would like the folder attributes set to hidden. How can I do this and will my program be able to insert and delete files when this attribute is set. ...
5
by: betterdie | last post by:
Dear guru I want to delete all file and folder recursivly under php code, can anyone give me commend for this. Thank very much
1
by: Do | last post by:
Hi, I'm having a small problem with a recursive vb function The output I'm looking to get is a string that looks like this Folder 1 > Folder 3 > Folder 5 Folder 5 is a child of 3; and Folder...
4
by: Elmo Watson | last post by:
Is there a way, with the System.IO class, to do a recursive list of a directory structure? For instance, in DirectoryInfo, you have GetDirectories and GetFiles .... In Directory, you have...
2
by: Jack Fox | last post by:
We are encountering a couple of problems with our ASP.NET / IIS 6.0 applications: In each of 3 production environments we maintain a Windows Server 2003 machine running NTFS as a file server....
10
by: AsheeG87 | last post by:
Hello Everyone! I have a linked list and am trying to include a recursive search. However, I am having trouble understanding how I would go about that. I don't quite understand a recursive...
4
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my function with a hardcoded file path --C:\Temp.txt the...
9
by: Lloyd Sheen | last post by:
For all those who don't think that a recursive search of files in folders is a good thing in the Microsoft.VisualBasic.FileIO.FileSystem namespace listen to this. I am reorg my mp3 collection. ...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.