Quick Recursive Delete?

S

Scott McNair

Is there a way to "instantly" delete a folder and all subfolders? I'm
currently trying:

System.IO.Directory.Delete(Filepath, True)

Each of these folders contains 60 subfolders which in turn contain about
100 files each (each of which is about 180kb). Because of this, my
application grinds to a halt.

Is there a way to simply flag a folder and its contents as deleted without
having to go through the recurse?
 
A

Alex Clark

Well, you could change the directory's "Hidden" attribute to make it less
visible, but that won't really solve it. Unfortunately the only way to
really delete something... is to delete it.

But if your app is grinding to a halt, why not just run the delete op on a
background worker thread?
 
G

Göran Andersson

Scott said:
Is there a way to "instantly" delete a folder and all subfolders? I'm
currently trying:

System.IO.Directory.Delete(Filepath, True)

Each of these folders contains 60 subfolders which in turn contain about
100 files each (each of which is about 180kb). Because of this, my
application grinds to a halt.

Is there a way to simply flag a folder and its contents as deleted without
having to go through the recurse?

No, the files are not really stored inside the folder on the disk, so
you can't remove them as a single unit.

If you would remove the folder without first deleting each file in it,
the files would become orphaned occupied space that you could neither
reach nor remove. The files would be lost, but you would not get the
disk space back.
 
S

Scott McNair

I have never tried this... but you got me thinking...what about not
"deleting" but moving the folder to the recycling bin....
I found this...
(again - I have never tried this, and do not know if this will work
with folders)
http://visualbasic.about.com/b/2006/12/22/recycle-files-with-vbnet-2005
.htm

Good find! I may use it for other applications. On this one, however, I
need the files to actually go away (to free up disc space) so simply
recycling them is not an option.

Thank you for the link though.
 
S

Scott McNair

But if your app is grinding to a halt, why not just run the delete op
on a background worker thread?

That's exactly what I wound up doing, thanks.

The application that creates the files does it via threading, so we may
have up to 32 different threads writing to the drive. Needless to say my
non-threaded delete application was unable to keep up (even disregarding
the hang-up issues).

By threading the deletes, the application seems to be deleting faster than
the files are being created, although just barely. But in this case,
barely is good enough (at least for now).
 
J

JB

If this file structure is not visible to the user, is there any real reason
that you need the subfolders? The whole thing will likely run faster if all
the files are in a single folder. Taken one step further, could you resign
to use a single file with offsets corresponding to the many files you now
have, or maybe a doc file with "subfolders" ( don't remember right now what
they are called) such as used by apps that implement embedding.
 

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