Results 1 to 3 of 3

Thread: Valid Windows Filename

  1. #1

    Thread Starter
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Valid Windows Filename

    So I don't have to reinvent the wheel, does anyone have a function (or is there an API function) that checks to see if a given string is a valid Windows filename (which may contain a drive letter and a network or non-network path)? The file need not exist at the time of user entry. This is straight validation of a user-entered string, not using the Common Dialog or any of the file controls.
    "It's cold gin time again ..."

    Check out my website here.

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Here is some code (very slightly modified) compliments of the VB5 setup program.
    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3.     If fValidFilename(Text1.Text) Then
    4.         MsgBox "OK"
    5.     Else
    6.         MsgBox "Nope"
    7.     End If
    8.  
    9. End Sub
    10. Public Function fCheckFNLength(strFilename As String) As Boolean
    11. '
    12. ' This routine verifies that the length of the filename strFilename is valid.
    13. ' Under NT (Intel) and Win95 it can be up to 259 (gintMAX_PATH_LEN-1) characters
    14. ' long.  This length must include the drive, path, filename, commandline
    15. ' arguments and quotes (if the string is quoted).
    16. '
    17.     fCheckFNLength = (Len(strFilename) < 260)
    18. End Function
    19. Public Function fValidFilename(strFilename As String) As Boolean
    20. '
    21. ' This routine verifies that strFileName is a valid file name.
    22. ' It checks that its length is less than the max allowed
    23. ' and that it doesn't contain any invalid characters..
    24. '
    25.     If Not fCheckFNLength(strFilename) Then
    26.         '
    27.         ' Name is too long.
    28.         '
    29.         fValidFilename = False
    30.         Exit Function
    31.     End If
    32.     '
    33.     ' Search through the list of invalid filename characters and make
    34.     ' sure none of them are in the string.
    35.     '
    36.     Dim iInvalidChar As Integer
    37.     Dim iFilename As Integer
    38.     Dim strInvalidChars As String
    39.    
    40.     ' I changed this to allow for : and \
    41.     strInvalidChars = "/*?""<>|" '"\/:*?""<>|"
    42.    
    43.     For iInvalidChar = 1 To Len(strInvalidChars)
    44.         If InStr(strFilename, Mid$(strInvalidChars, iInvalidChar, 1)) <> 0 Then
    45.             fValidFilename = False
    46.             Exit Function
    47.         End If
    48.     Next iInvalidChar
    49.    
    50.     fValidFilename = True
    51.    
    52. End Function

  3. #3

    Thread Starter
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    Thanks, Marty. This will be suitable with some modifications - such as, if the string contains ":", then it must only be part of the first two characters as a drive specification; also, if it contains two consecutive backslashes, then they must be the first two characters of the string. I was fairly sure of the rules (I thought the max length was 255 rather than 259, and I knew about the invalid characters). I was just wondering if there was any more to it. Thanks for the legwork.
    "It's cold gin time again ..."

    Check out my website here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width