dcsimg
Results 1 to 3 of 3
  1. #1

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Resolved [RESOLVED] Deleting - Files And Sub directories Without Deleting The Folder Itself

    Hello i have a question how can i delete all files and folders within a folder but not delete the main folder that contains the items!!
    this code below will delete everything but also deletes the main folder so i have to remake a empty folder for the one i deleted any way around this? thanks!!!

    Code:
        Public Function DirExists(ByVal Path As String) As Boolean
            If My.Computer.FileSystem.DirectoryExists(Path) Then
                Return True
            Else
                Return False
            End If
        End Function
    
    
      Public Function DeletePath(ByVal Path As String)
            Try
                If DirExists(Path) Then
                    Application.DoEvents()
                    My.Computer.FileSystem.DeleteDirectory(Path, FileIO.DeleteDirectoryOption.DeleteAllContents)
                End If
    
                If Not DirExists(Path) Then
                    Application.DoEvents()
                    My.Computer.FileSystem.CreateDirectory(Path)
                End If
            Catch ex As Exception
                'err
            End Try
        End Function

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,928

    Re: Deleting - Files And Sub directories Without Deleting The Folder Itself

    vb.net Code:
    1. Public Sub DeleteFilesAndSubfolders(folderPath As String)
    2.     If Directory.Exists(folderPath) Then
    3.         For Each filePath As String In Directory.GetFiles(folderPath)
    4.             File.Delete(filePath)
    5.         Next
    6.  
    7.         For Each subfolderPath As String In Directory.GetDirectories(folderPath)
    8.             Directory.Delete(subfolderPath, True)
    9.         Next
    10.     End If
    11. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,928

    Re: Deleting - Files And Sub directories Without Deleting The Folder Itself

    By the way, if you feel like you need to use Application.DoEvents then you should be looking at doing asynchronous/background work instead, e.g. use a BackgroundWorker and do the work in the DoWork event handler.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.