Function IsFilePathNotValid(FilePath As String) As Byte
' formats
' <=255 chars
' split at \
' each elemnt
' not null apart than first
' none of these chars
' 1-32 / \ " * ; < > ? |
' and : unless first
' and . not first or last character
' none of these names
' .lock, CON, PRN, AUX, NUL, COM0-COM9, LPT0-LPT9, *_vti_*, desktop.ini, any filename starting with ~$
' forms when in the root(?)
' Return is NOT Valid value
' 0 Valid
' 1 Too Long
' 2 Element is null
' 3 Only first element can have a :
' 4 Invalid character found
' 5 First or last character cannot be .
' 6 Illegal Name or partial name
' 7 Forms cannot be in the root
' 8 Drive has invalid format
' 9 Drive does not exist
' 10
Dim i As Integer
Dim j As Integer
Dim components() As String
' Assume false
IsFilePathNotValid = 0
' Not too long
If Len(FilePath) > 255 Then
IsFilePathNotValid = 1
Exit Function
End If
' Split into components (sub directories etc.)
components = Split(FilePath, "\")
' Scan each element, note the first and last have some special rules
For i = 0 To UBound(components)
' Only first item can be empty
If components(i) = "" And i <> 0 Then
IsFilePathNotValid = 2
Exit Function
End If
' Only first item can have a :
If InStr(1, components(i), ":") > 0 And i > 0 Then
IsFilePathNotValid = 3
Exit Function
End If
' Invalid Character present
For j = 1 To Len(components(i))
If Mid(components(i), j, 1) <= " " _
Or InStr(1, "/\""*;<>?|", Mid(components(i), j, 1)) <> 0 _
Then
IsFilePathNotValid = 4
Exit Function
End If
Next
' First or last character cannot be .
If Left(components(i), 1) = "." _
Or Right(components(i), 1) = "." _
Then
IsFilePathNotValid = 5
Exit Function
End If
' Invalid names
If components(i) = ".Lock" _
Or components(i) = "CON" _
Or components(i) = "PRN" _
Or components(i) = "AUX" _
Or components(i) = "NUL" _
Or components(i) Like "COM#" _
Or components(i) Like "PRN#" _
Or components(i) Like "*_vti_*" _
Or components(i) Like "desktop.ini" _
Or components(i) Like "-$*" _
Then
IsFilePathNotValid = 6
Exit Function
End If
Next
' Special case - Forms not in root
If InStr(1, components(0), ":") > 0 _
And components(2) = "forms" _
Then
IsFilePathNotValid = 7
Exit Function
End If
' Check Drive is valid
If InStr(1, components(0), ":") > 0 Then
If Len(components(0)) <> 2 Then
IsFilePathNotValid = 8
Exit Function
End If
If fileSystem Is Nothing Then Set fileSystem = CreateObject("Scripting.FileSystemObject")
If Not fileSystem.DriveExists(Left$(components(0), 1)) Then
IsFilePathNotValid = 9
Exit Function
End If
End If
' Further tests when figured out or needed
End Function