Skipping Directories
Skipping Directories
Skipping Directories
Skipping Directories
Skipping Directories
Skipping Directories Skipping Directories Skipping Directories Skipping Directories Skipping Directories Skipping Directories Skipping Directories Skipping Directories
Skipping Directories Skipping Directories
Skipping Directories
Go Back  Xtreme Visual Basic Talk > > > Skipping Directories


Reply
 
Thread Tools Display Modes
  #1  
Old 01-11-2012, 08:19 PM
jaden2001 jaden2001 is offline
Junior Contributor
 
Join Date: Apr 2005
Location: Albany, GA
Posts: 235
Default Skipping Directories


I am creating a program to clean out files in directories such as My Documents, My Pictures, etc. I have ran into an issue. If the file is denied access I want it to skip the directory and continue instead of throwing an error. Here is what I am working with.

Code:
Public Sub DeleteFiles(sDirectory As String)
        Try
            'Delete loose end files in the directory
            For Each strFile As String In Directory.GetFiles(sDirectory)
                IO.File.Delete(strFile)
            Next
           'Finds directories in choosen folder
            For Each Dir As String In Directory.GetDirectories(sDirectory)
                'Search through all the subfolders and deletes them
                For Each Dir2 In Directory.GetDirectories(Dir)
                    Directory.Delete(Dir2, True)
                Next
                Directory.Delete(Dir, True)
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
Happy Programming,

Cody
__________________
Da Vinci - "Sleep is but a waste of time"
Reply With Quote
  #2  
Old 01-12-2012, 03:32 AM
4x2y 4x2y is offline
Contributor
 
Join Date: Sep 2005
Posts: 566
Default

Enclose each Delete inside Try, something like this
Code:
Public Sub DeleteFiles(ByVal sDirectory As String) Try 'Delete loose end files in the directory For Each strFile As String In Directory.GetFiles(sDirectory) Try IO.File.Delete(strFile) Catch ex As Exception MsgBox(ex.Message) End Try Next 'Finds directories in choosen folder For Each Dir As String In Directory.GetDirectories(sDirectory) 'Search through all the subfolders and deletes them For Each Dir2 In Directory.GetDirectories(Dir) Try Directory.Delete(Dir2, True) Catch ex As Exception MsgBox(ex.Message) End Try Next Try Directory.Delete(Dir, True) Catch ex As Exception MsgBox(ex.Message) End Try Next Catch ex As Exception MsgBox(ex.Message) End Try End Sub
__________________
Visit my site
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Skipping Directories
Skipping Directories
Skipping Directories Skipping Directories
Skipping Directories
Skipping Directories
Skipping Directories Skipping Directories Skipping Directories Skipping Directories Skipping Directories Skipping Directories Skipping Directories
Skipping Directories
Skipping Directories
 
Skipping Directories
Skipping Directories
 
-->