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?
JOSHSKORNAsked:
Who is Participating?
 
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
 
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
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
 
JOSHSKORNAuthor Commented:
doraiswamy...I noticed his reply wasn't completely correct.  See my reply to his.
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.