简体   繁体   中英

Checking if the first part of a directory already exists

I have created an application that creates a folder and a few more documents for a new site the User is surveying.

Once the User has entered the necessary Site details, a check is done to see if the folder exists, and if it doesn't, it creates it.

Here is a sample of the code I am using to achieve this:

Public Class Form1

    Dim SiteName As String
    Dim SiteNumber As String

    Private Sub btnCreateFolder_Click(sender As Object, e As EventArgs) _
        Handles btnCreateFolder.Click

        SiteName = txtSiteName.Text
        SiteNumber = txtSiteNumber.Text

        CurrentSiteLoc = "C:\VBA\" & SiteNumber & " " & SiteName

        If Not IO.Directory.Exists(CurrentSiteLoc) Then
            MkDir(CurrentSiteLoc)

        Else
            MessageBox.Show("Folder already exists.")

        End If

    End Sub

End Class

This check works perfectly if the User always uses the correct SiteName, however each site is defined by its Site Number.

"524128 Corner's Stone"

This is an example of a possible site folder name, but the User might also decide to add some more info so it could be created with the name:

"524128 Corner's Stone (L6)"

What is the best way to search my directory for a folder that has the same Site Number, instead of the same folder name?

You can use the search pattern argument of the Directory.GetDirectories() -method:

If System.IO.Directory.GetDirectories("C:\VBA\", SiteNumber & " *").Count = 0 Then
    MkDir(CurrentSiteLoc)
Else
    MessageBox.Show("Folder already exists.")
End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM