473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 8292
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.