Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 48
Default Is this code error-bearable

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

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 10,593
Default Is this code error-bearable

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)

"ashish128" wrote in message
oups.com...
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



  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 48
Default Is this code error-bearable

Thanks Bob
It worked as a charm

Bob Phillips wrote:
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)

"ashish128" wrote in message
oups.com...
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


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
2 Questions John Calder New Users to Excel 18 August 24th 06 04:17 AM
code not unique find latest date Barbara Wiseman Excel Discussion (Misc queries) 3 December 11th 05 08:50 AM
VLOOKUP for Zip Code Ranges JerseyJR Excel Worksheet Functions 2 September 6th 05 06:37 PM
Conform a total to a list of results? xmaveric Excel Discussion (Misc queries) 1 August 21st 05 07:22 PM
Macro for changing text to Proper Case JPriest Excel Worksheet Functions 3 August 8th 05 09:31 PM


All times are GMT +1. The time now is 06:46 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"