EvalFileName - Ensure that the basename of a file or directory is valid
' EvalFileName ensures that the basename of a file or directory
' conforms to the Microsoft file naming guidelines (see MSDN webpage)
' http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/
' naming_a_file.asp
'
' Return value:
' True = Valid basename
' False = Invalid basename
'
' Note: Since EvalFileName checks only the basename of a file or
' directory, it does not test the max filename length condition
' (ANSI 255 for files, 248 for directories, including the path)
' This error is however trapped by the APIMakeDirectory function
'
' Example:
' MsgBox EvalFileName("test.txt")
' MsgBox EvalFileName("inva:lid.txt")
Private Function EvalFileName(ByVal Name As String) As Boolean
Dim i As Long
Dim Test As String
Const BAD_FILENAME_CHARS As String = """" & "/" & "\" & ":" & "|" & "<" & _
">" & "*" & "?"
' We need a name
If Len(Trim$(Name)) = 0 Then
Exit Function
End If
' Test trailing space or period
Test = Right$(Name, 1)
If Test = " " Or Test = "." Then
Exit Function
End If
' Test illegal and non-printable characters
Test = BAD_FILENAME_CHARS
For i = 0 To 31
Test = Test & Chr$(i)
Next
For i = 1 To Len(Name)
If InStr(1, Test, Mid$(Name, i, 1)) > 0 Then
Exit Function
End If
Next
' Test possible use of reserved words
' (CON, PRN, AUX, CLOCK$, NUL)
If LCase$(Name) = "con" Or LCase$(Name) = "prn" Or LCase$(Name) = "aux" Or _
LCase$(Name) = "clock$" Or LCase$(Name) = "nul" Then
Exit Function
End If
' COM/LPT (1-9)
For i = 1 To 9
If LCase$(Name) = "com" & CStr(i) Or LCase$(Name) = "lpt" & CStr(i) Then
Exit Function
End If
Next
' All tests clear, return success
EvalFileName = True
End Function