• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

How do I delete all files from a removable drive?

I have the following code:

Dim pstrDrive As String
pstrDrive = cboDrives.Text.Substring(0, 3)
MessageBox.Show("Delete Drive: " & pstrDrive, "Delete")  'pstrDrive will show, for example:  E:\
Directory.Delete(pstrDrive, True)

With this code, I receive an error message, "Absolute path information required."

I want to be able to delete EVERYTHING on the E:\ drive, all files, directories and sub directories.  Can someone help me out?
0
JOSHSKORN
Asked:
JOSHSKORN
  • 3
  • 3
  • 2
1 Solution
 
william007Commented:
Followed will do it.

        Dim f As DirectoryInfo = New DirectoryInfo("E:\")
        Dim fs As DirectoryInfo()
        fs = f.GetDirectories
        Dim i As Integer
        For i = 0 To fs.Length - 1
            Directory.Delete(fs(i).FullName, True)
        Next
0
 
doraiswamyCommented:
Hi Joshkorn,

William007 is partly correct.
Directory.Delete(pathname, TRUE) deletes directories reursively ONLY when subdirectories are empty. So first you would have to recurse through all the directories and delete all the files.
0
 
JOSHSKORNAuthor Commented:
Thanks for the reply, William007 but the code does not work.

Some further information, I'm extracting the drive name from the selected value of a combo box.  The code I use to extract "E:\" (originally it's "E:\ - Removable" but I use the substring to extract the first 3 characters) works great.

I've put your code together with my code.  I see no results.  I've even separated your code from my code and explicity used "E:\" as the DirectoryInfo parameter (which is in your example) instead of using a variable (which would be my modification) and nothing happens.  I noticed that the For loop seems to not be executing.

Here's the code I'm trying to execute with William007's input modified.

Dim pstrDrive As String
pstrDrive = cboDrives.Text.Substring(0, 3)   'The text for cboDrives would be either "A:\ - Removable" or "E:\ - Removable".  This code correctly extracts the 1st three characters from that text.
Dim f As DirectoryInfo = New DirectoryInfo(pstrDrive)

Dim fs As DirectoryInfo()
fs = f.GetDirectories
Dim i As Integer

For i = 0 To fs.Length - 1
     Directory.Delete(fs(i).FullName, True)
Next
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
JOSHSKORNAuthor Commented:
doraiswamy...I noticed his reply wasn't completely correct.  See my reply to his.
0
 
william007Commented:
Sorry, forget to delete file, just concentrating on deleting directory;)

Followed is the corrected code
        Dim f As DirectoryInfo = New DirectoryInfo("E:\")

        Dim fs As DirectoryInfo()
        fs = f.GetDirectories

        Dim i, j As Integer

        For i = 0 To fs.Length - 1
            Directory.Delete(fs(i).FullName, True)
        Next

        Dim fi As FileInfo()
        fi = f.GetFiles
        For j = 0 To fi.Length - 1
            File.Delete(fi(j).FullName)
        Next
0
 
JOSHSKORNAuthor Commented:
Thanks William007.
0
 
william007Commented:
Welcome :-)
0
 
doraiswamyCommented:
This has to be done recursively as follows:

Have the function del_dir:

    Private Sub del_dir(ByVal dirpath As String)
        ' this sub deletes all files in dirpath
        ' Then for each subdirectory in dirpath, it calls itself and deletes the subdirectory

        Dim f As IO.DirectoryInfo = New IO.DirectoryInfo(dirpath)
        Dim fs As IO.DirectoryInfo
        Dim fi() As IO.FileInfo
        Dim fn() As String
        Dim fname As String
        Dim dirname As String
        fi = f.GetFiles("*.*")
        'delete all files in this subdirectory
        Dim i As Integer
        For i = 0 To fi.Length - 1
            fname = fi(i).FullName
            IO.File.Delete(fname)
        Next

        ' now go to all subdirectories
        fn = IO.Directory.GetDirectories(dirpath)
        For Each dirname In fn
            del_dir(dirname)
            IO.Directory.Delete( dirname)
        Next
    End Sub


And call it from anywhere with the root path e.g. del_dir("E:\")
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now