A way to check user input is valid folder name?

Jaymond Flurrie

Well-known Member
Joined
Sep 22, 2008
Messages
921
Office Version
  1. 365
Platform
  1. Windows
Would there be some easy method (for example regular expression would do the trick, but I'm not fluent with them) to check that user gave me on input box something that I can use as a folder name? I know how to capture the string, but validating is the problem here.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
The below function is a direct way to see if string would be a valid folder name. It tries to create the folder using the provide name in the temp directory if it already exist or was created successfully it returns true.

Code:
Function ValidFolderName(folderName As String) As Boolean

    Dim TestName As String
    Dim str As String

    TestName = Environ("Temp") & "\" & folderName
    On Error Resume Next
    str = Dir(TestName, vbDirectory)
    '// To Prevent existing folder from being deleted
    If str <> "" Then
        ValidFolderName = True
        Exit Function
    End If
    '// Try and make a directory with provided name
    MkDir (TestName)
    '// True if directory was created
    ValidFolderName = Dir(TestName, vbDirectory) <> ""
    '// Delete if directory was created to avoid clutter.
    If ValidFolderName Then RmDir TestName
    On Error GoTo 0
End Function
 
Upvote 0
You could use something like this (which I think originally came from XtremeVBTalk):
Code:
Public Function IsInvalidFileName(ByVal FileName As String) As Boolean

    Dim re As Object
    Set re = CreateObject("VBScript.RegExp")
    'forbidden characters in file names:
    re.Pattern = "[\\/:\*\?""<>\|]"
    IsInvalidFileName = (re.Execute(FileName).Count > 0)
    Set re = Nothing

End Function
 
Upvote 0
Ralajer's solution seems more for those who wants to understand everything in there, while Rory's is more flexible (if I don't want "a" in the name for some reason, Rory's solution is easier to modify).

Anyway, I think that both are great answers, thanks!
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top