VBA UDF (User Defined Function) to determine if a file name is valid, e.g.
If IsValidFileName("test.txt") Then
Msgbox "File name is valid"
Else
Msgbox "File name is NOT valid."
End If
I was working on some code that would save an Excel file and thought it would be good to check if the file name was actually a valid name. So, I found an article about naming files and used the info in it to write a formula to check if a file or folder name is a legal file name. I was thinking whether to name it 'IsValidFileFolderName' or 'IsValidFileName'. I decided on the shorter version. See the quote below for why.
From 'Naming Files, Paths, and Namespaces:
Note that a directory is simply a file with a special attribute designating it as a directory, but otherwise must follow all the same naming rules as a regular file. Because the term directory simply refers to a special type of file as far as the file system is concerned, some reference material will use the general term file to encompass both concepts of directories and data files as such. Because of this, unless otherwise specified, any naming or usage rules or examples for a file should also apply to a directory.
The code below takes a string and determines if it is a legal file name or folder name.