none
Delete everything except certain files from folder RRS feed

  • Question

  • Hello,

    Found this to remove all files from a folder and sub folders, that works .

    But some file's ( about 15 i want to keep) how do i do that ?

    This is the code for removing

      Private Sub DeleteDirectory(path As String)
            If Directory.Exists(path) Then
                
                For Each filepath As String In Directory.GetFiles(path)
                    File.Delete(filepath)
                Next
                
                For Each dir As String In Directory.GetDirectories(path)
                    DeleteDirectory(dir)
                Next
                
            End If
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim path As String = "D:\Testweg"
            DeleteDirectory(path)
        End Sub

    Saturday, May 25, 2019 5:32 PM

Answers

  • Hi

    Here is an example.

    ' Form1 Button2 and
    ' ListBox1 (just for display of
    ' protected files)
    Option Strict On
    Option Explicit On
    Imports System.IO
    
    Public Class Form1
    	Dim KnownFN As New List(Of String)
    
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		With KnownFN
    			.Add("C:\Users\lesha\Desktop\New folder\RP9.jpg")
    			.Add("C:\Users\lesha\Desktop\New folder\Folder2\BishopsAvenue.pdf")
    		End With
    		ListBox1.DataSource = KnownFN
    	End Sub
    	Private Sub DeleteDirectory(path As String)
    		If Directory.Exists(path) Then
    
    			For Each filepath As String In Directory.GetFiles(path)
    				If KnownFN.Contains(filepath) Then
    					Continue For
    				End If
    				File.Delete(filepath)
    			Next
    
    			For Each dir As String In Directory.GetDirectories(path)
    				DeleteDirectory(dir)
    			Next
    
    		End If
    	End Sub
    	Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles Button2.Click
    		Dim path As String = "C:\Users\lesha\Desktop\New folder"
    		DeleteDirectory(path)
    	End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Florishilte Sunday, May 26, 2019 11:26 AM
    Saturday, May 25, 2019 6:52 PM

All replies

  • Hi

    If you know the names (paths) of the files you want to keep then just add them to a List(Of String) and check if the List.Contains(filepath) and if not then go ahead and Delete - otherwise skip the Delete.


    Regards Les, Livingston, Scotland

    Saturday, May 25, 2019 5:56 PM
  • You should be able to use Directory.Delete

    Directory.Delete("D:\Testweg",True)


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, May 25, 2019 6:13 PM
    Moderator
  • Les,

    That is what i am looking for , i know the names of the files i want to keep.

    Do you have a example ?

    Saturday, May 25, 2019 6:30 PM
  • Hi

    Here is an example.

    ' Form1 Button2 and
    ' ListBox1 (just for display of
    ' protected files)
    Option Strict On
    Option Explicit On
    Imports System.IO
    
    Public Class Form1
    	Dim KnownFN As New List(Of String)
    
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		With KnownFN
    			.Add("C:\Users\lesha\Desktop\New folder\RP9.jpg")
    			.Add("C:\Users\lesha\Desktop\New folder\Folder2\BishopsAvenue.pdf")
    		End With
    		ListBox1.DataSource = KnownFN
    	End Sub
    	Private Sub DeleteDirectory(path As String)
    		If Directory.Exists(path) Then
    
    			For Each filepath As String In Directory.GetFiles(path)
    				If KnownFN.Contains(filepath) Then
    					Continue For
    				End If
    				File.Delete(filepath)
    			Next
    
    			For Each dir As String In Directory.GetDirectories(path)
    				DeleteDirectory(dir)
    			Next
    
    		End If
    	End Sub
    	Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles Button2.Click
    		Dim path As String = "C:\Users\lesha\Desktop\New folder"
    		DeleteDirectory(path)
    	End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Florishilte Sunday, May 26, 2019 11:26 AM
    Saturday, May 25, 2019 6:52 PM
  • Thanks Les,

    Works great.

    Sunday, May 26, 2019 11:26 AM