Delete Hidden Directories & wildcards search in VB.net?

~OSD~ 2,131 Reputation points
2020-12-14T14:44:03.417+00:00

Hi,

I would like to delete directories (and all it's contents) using VB.Net where I know part of the directory name. For example, see below:
All directories with name default***** should be deleted.
All directories with name Trash should be deleted (even if it were hidden folder).
All directories with name HiddenDir should be deleted (even if the folder is hidden, but if the name match it should be deleted regardless if it's hidden or not.)

48003-image.png

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,644 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Anonymous
    2020-12-14T18:08:53.367+00:00

    Hi OK, I find it a weird way to do things, but, here is some code that does what you ask for. The more you try to itemize things then code gets messy. This is an example only! To try this, you MUST create a TEST Directory structure. (I also added a folder called KEEP where I copied all from main directory so that I could recreate the structure while testing) as this code will delete things. Initial List ![47997-1.png][1] Selected for Deletion ![47998-2.png][2] Code ' Form1 with ListBox1 ' Button1 and Button2 ' ****************** ' NOTE: this example should ' ONLY be tried on a test ' Directory structure ' ****************** Option Strict On Option Explicit On Imports System.IO Public Class Form1 Dim basefolder As String = Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "TEST1") Dim dirs() As String = Nothing Dim ToDelete As New List(Of String) Dim matchname() As String = {"DeleteMe", "DeleteMe1", "DeleteMe2"} Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load dirs = Directory.GetDirectories(basefolder, "*", SearchOption.AllDirectories) ListBox1.DataSource = dirs End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click MakeDeletionsList() ListBox1.DataSource = ToDelete End Sub Sub MakeDeletionsList() ToDelete.Clear() For Each s As String In dirs Dim di As New DirectoryInfo(s) If (di.Attributes And FileAttributes.Directory) = FileAttributes.Directory Then ' here we have a Directory ' ignore my KEEP folder If Not di.FullName.Contains("KEEP") Then If (di.Attributes And FileAttributes.Hidden) = FileAttributes.Hidden Then ' here we have a Hidden Dir ' so delete it ToDelete.Add(di.FullName) End If If di.Name.StartsWith("default") Or di.Name = ("Trash") Or di.Name.StartsWith("HiddenDir") Or matchname.Contains(di.Name) Then If Not ToDelete.Contains(di.FullName) Then ToDelete.Add(di.FullName) End If End If End If Next End Sub Sub DoActualDeletions() Dim deleted As New List(Of String) For Each s As String In ToDelete Directory.Delete(s) deleted.Add(s) Next For Each s As String In deleted ToDelete.Remove(s) Next End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click ' issue ALL SORTS of dire ' WARNINGS DoActualDeletions() ListBox1.DataSource = Nothing ListBox1.DataSource = ToDelete End Sub End Class [1]: /api/attachments/47997-1.png?platform=QnA [2]: /api/attachments/47998-2.png?platform=QnA

    0 comments No comments

  2. Xingyu Zhao-MSFT 5,356 Reputation points
    2020-12-16T07:43:18.327+00:00

    Hi @~OSD~ ,

    You can consider using Regex to match directory names and then delete all the matched directories.
    Here's the code you can refer to.

            Dim path As String = "your directory path"  
      
            Dim pattern As String = "^(default|HiddenDir|Trash)" ' ^ matches start of line'  
            Dim regex = New Regex(pattern)  
            Dim directories = Directory.GetDirectories(path).Where(Function(dic) regex.IsMatch(dic.Remove(0, path.Length)))  
            For Each d In directories  
                Dim dir = New DirectoryInfo(d)  
                dir.Delete(True)  
            Next  
    

    My folders before the test.
    48618-1.png
    Result of my test.
    48681-2.png

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments