dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: VB6
Expertise: beginner
Nov 25, 2002

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


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
Pan Polychronopoulos
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date