Here is some code (very slightly modified) compliments of the VB5 setup program.
VB Code:
Private Sub Command1_Click()
If fValidFilename(Text1.Text) Then
MsgBox "OK"
Else
MsgBox "Nope"
End If
End Sub
Public Function fCheckFNLength(strFilename As String) As Boolean
'
' This routine verifies that the length of the filename strFilename is valid.
' Under NT (Intel) and Win95 it can be up to 259 (gintMAX_PATH_LEN-1) characters
' long. This length must include the drive, path, filename, commandline
' arguments and quotes (if the string is quoted).
'
fCheckFNLength = (Len(strFilename) < 260)
End Function
Public Function fValidFilename(strFilename As String) As Boolean
'
' This routine verifies that strFileName is a valid file name.
' It checks that its length is less than the max allowed
' and that it doesn't contain any invalid characters..
'
If Not fCheckFNLength(strFilename) Then
'
' Name is too long.
'
fValidFilename = False
Exit Function
End If
'
' Search through the list of invalid filename characters and make
' sure none of them are in the string.
'
Dim iInvalidChar As Integer
Dim iFilename As Integer
Dim strInvalidChars As String
' I changed this to allow for : and \
strInvalidChars = "/*?""<>|" '"\/:*?""<>|"
For iInvalidChar = 1 To Len(strInvalidChars)
If InStr(strFilename, Mid$(strInvalidChars, iInvalidChar, 1)) <> 0 Then
fValidFilename = False
Exit Function
End If
Next iInvalidChar
fValidFilename = True
End Function