Is this code error-bearable

A

ashish128

Hello All,
following is a code to delete all contents of Two drives.But
it is not working if first drive is not found. Is there a possibility
to bypass the code if the drive is not there in system or any code to
find if there is <drive letter> drive in the system.
---------------- Careful, Either change drive letter to non exixtent
ones or create virtual drives for experiment-------------------

Dim FSO As Object
Dim MyPath As String



Set FSO = CreateObject("scripting.filesystemobject")



MyPath = "i:\" '<< Change



If Right(MyPath, 1) = "\" Then
MyPath = Left(MyPath, Len(MyPath) - 1)
End If



If FSO.FolderExists(MyPath) = False Then
MsgBox MyPath & " doesn't exist"
Exit Sub
End If



On Error Resume Next
'Delete files
FSO.deletefile MyPath & "\*.*", True
'Delete subfolders
FSO.deletefolder MyPath & "\*.*", True
On Error GoTo 0


MyPath = "g:\" '<< Change



If Right(MyPath, 1) = "\" Then
MyPath = Left(MyPath, Len(MyPath) - 1)
End If



If FSO.FolderExists(MyPath) = False Then
MsgBox MyPath & " doesn't exist"
Exit Sub
End If



On Error Resume Next
'Delete files
FSO.deletefile MyPath & "\*.*", True
'Delete subfolders
FSO.deletefolder MyPath & "\*.*", True
On Error GoTo 0
 
B

Bob Phillips

Is this what you mean?

Sub FileDelete()
Dim FSO As Object

Set FSO = CreateObject("scripting.filesystemobject")

DeleteFiles FSO, "x:\" '<< Change drive to suit

DeleteFiles FSO, "g:\" '<< Change

Set FSO = Nothing

End Sub

Sub DeleteFiles(FSO As Object, MyPath As String)
If Right(MyPath, 1) = "\" Then
MyPath = Left(MyPath, Len(MyPath) - 1)
End If

If FSO.FolderExists(MyPath) Then

On Error Resume Next
'Delete files
FSO.deletefile MyPath & "\*.*", True
'Delete subfolders
FSO.deletefolder MyPath & "\*.*", True
On Error GoTo 0

Else
MsgBox MyPath & " doesn't exist"

End If

End Sub


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 
A

ashish128

Thanks Bob
It worked as a charm

Bob said:
Is this what you mean?

Sub FileDelete()
Dim FSO As Object

Set FSO = CreateObject("scripting.filesystemobject")

DeleteFiles FSO, "x:\" '<< Change drive to suit

DeleteFiles FSO, "g:\" '<< Change

Set FSO = Nothing

End Sub

Sub DeleteFiles(FSO As Object, MyPath As String)
If Right(MyPath, 1) = "\" Then
MyPath = Left(MyPath, Len(MyPath) - 1)
End If

If FSO.FolderExists(MyPath) Then

On Error Resume Next
'Delete files
FSO.deletefile MyPath & "\*.*", True
'Delete subfolders
FSO.deletefolder MyPath & "\*.*", True
On Error GoTo 0

Else
MsgBox MyPath & " doesn't exist"

End If

End Sub


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 

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

Similar Threads

Need help editing this code 8
open all files in directory 2
Convert Multiple CSV Files to XLS Files (Again) 2
Windows XP vba code help 10
on error help 4
Open CSV File 4
Folder Button 2
F.A.O Mike H 3

Top