Here's an update to my IsValidFilename routine. It Fixed several holes
(clock$, prn, nul, com5-9 and lpt5-9) and there's a bit of a speed
optimization by checking the length of Filename when testing for
reserved words. - Jim
Public Function IsValidFilename(ByVal Filename As String) As Boolean
'*****************************************************************
'* NAME: IsValidFilename *
'* *
'* PURPOSE: To determine whether a given filename is a valid *
'* filename *
'* *
'* PARAMETER: Filename The filename you wish to validate. *
'* The filename must be by its self *
'* (i.e. no path). *
'*****************************************************************
Const strDBL_QUOTE As String = """"
Dim lngLength As Long
Dim x As Long
lngLength = Len(Filename)
'make sure filename isn't blank
If Len(Filename) = 0 Then
Exit Function
End If
'test for illegal characters
For x = 1 To Len(Filename)
If InStr(1, "/<>?\|*:" & strDBL_QUOTE, Mid$(Filename, x, 1),
vbBinaryCompare) > 0 Then
Exit Function
End If
Next x
'test for resevered words
If StrComp(Right$(Filename, 1), ".", vbBinaryCompare) = 0 Then 'if
last char is .
Filename = Left$(Filename, Len(Filename) - 1)
'remove .
End If
Select Case Len(Filename)
Case 3
If StrComp(Filename, "aux", vbTextCompare) = 0 Then
Exit Function
End If
If StrComp(Filename, "con", vbTextCompare) = 0 Then
Exit Function
End If
If StrComp(Filename, "prn", vbTextCompare) = 0 Then
Exit Function
End If
If StrComp(Filename, "nul", vbTextCompare) = 0 Then
Exit Function
End If
Case 4
'test for com1-9
If StrComp(Left$(Filename, 3), "com", vbTextCompare) = 0 Then
If InStr(1, "123456789", Right$(Filename, 1), vbBinaryCompare)
Quote:
> 0 Then
Exit Function
End If
End If
'test for lpt1-9
If StrComp(Left$(Filename, 3), "lpt", vbTextCompare) = 0 Then
If InStr(1, "123456789", Right$(Filename, 1), vbBinaryCompare)
Quote:
> 0 Then
Exit Function
End If
End If
Case 6
If StrComp(Filename, "clock$", vbTextCompare) = 0 Then
Exit Function
End If
End Select
'if it got this far, filename is valid
IsValidFilename = True
End Function
Quote:
> > Hi !
> > How can I verify if a FileName is a valid FileName....?
> > Thanks!
> > JP
> Here's my own person IsValidFilename routine. I can't say that it's
> been perfectly tested, so if you find a bug, please let me know.
> Public Function IsValidFilename(ByVal Filename As String) As Boolean
> '*****************************************************************
> '* NAME: IsValidFilename *
> '* *
> '* PURPOSE: To determine whether a given filename is a valid *
> '* filename *
> '* *
> '* PARAMETER: Filename The filename you wish to validate. *
> '* The filename must be by its self *
> '* (i.e. no path). *
> '*****************************************************************
> Const strDBL_QUOTE As String = """"
> Dim x As Long
> 'make sure Filename isn't blank
> If Len(Filename) = 0 Then
> Exit Function
> End If
> 'test for illegal characters
> For x = 1 To Len(Filename)
> If InStr(1, "/<>?\|*:" & strDBL_QUOTE, Mid$(Filename, x, 1),
> vbBinaryCompare) > 0 Then
> Exit Function
> End If
> Next x
> 'test for resevered words
> If StrComp(Right$(Filename, 1), ".", vbBinaryCompare) = 0 Then
'if
> last char is .
> Filename = Left$(Filename, Len(Filename) -
> 1) 'remove .
> End If
> If StrComp(Filename, "aux", vbTextCompare) = 0 Then
> Exit Function
> End If
> If StrComp(Filename, "com1", vbTextCompare) = 0 Then
> Exit Function
> End If
> If StrComp(Filename, "com2", vbTextCompare) = 0 Then
> Exit Function
> End If
> If StrComp(Filename, "com3", vbTextCompare) = 0 Then
> Exit Function
> End If
> If StrComp(Filename, "com4", vbTextCompare) = 0 Then
> Exit Function
> End If
> If StrComp(Filename, "lpt1", vbTextCompare) = 0 Then
> Exit Function
> End If
> If StrComp(Filename, "lpt2", vbTextCompare) = 0 Then
> Exit Function
> End If
> If StrComp(Filename, "lpt3", vbTextCompare) = 0 Then
> Exit Function
> End If
> If StrComp(Filename, "lpt4", vbTextCompare) = 0 Then
> Exit Function
> End If
> If StrComp(Filename, "con", vbTextCompare) = 0 Then
> Exit Function
> End If
> IsValidFilename = True
> End Function
> Sent via Deja.com http://www.deja.com/
> Before you buy.
Sent via Deja.com http://www.deja.com/
Before you buy.