Valid FileName 
Author Message
 Valid FileName

Hi !

How can I verify if a FileName is a valid FileName....?

Thanks!

JP

* Sent from RemarQ http://www.*-*-*.com/ The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!



Sat, 01 Jun 2002 03:00:00 GMT  
 Valid FileName
One way is.... Try to open it for input and have an error trap
Quote:

> Hi !

> How can I verify if a FileName is a valid FileName....?

> Thanks!

> JP

> * Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
> The fastest and easiest way to search and participate in Usenet - Free!



Sat, 01 Jun 2002 03:00:00 GMT  
 Valid FileName


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.



Sun, 02 Jun 2002 03:00:00 GMT  
 Valid FileName

Quote:
> How can I verify if a FileName is a valid FileName....?

I like opening the file and testing it's length.  If it's zero then it's not a
file, then kill it.  Not good for 0 byte files though, and if you're checking
for files of any size don't use integers like I show:

Function fexist(finame As String)
  io% = FreeFile
  Open finame For Random As #io%
  fexist = LOF(io%)
  Close #io%
End Function
..------------------------------..

Private Sub Command1_Click()
  a% = fexist("test.dat")
  If a% = 0 Then
    Print "file doesn't exist"
    Kill "test.dat"
  Else
    Print "file is a valid file"
  End If
End Sub

-Jana



Sun, 02 Jun 2002 03:00:00 GMT  
 Valid FileName

Quote:

>How can I verify if a FileName is a valid FileName....?

As most of the other respondents mentioned, the safest
way is to either attempt to open the file for input or create
the file.

The thing is, the Win32 API specifies very little about what
is a legal file name; it's almost entirely file system dependent.

And with 3 different basic file systems (FAT16, FAT32, and
NTFS) plus the possibility of additional filesystems on
non-Windows machines in a networked environment,
you have a tremendous number of possibilities.



Sun, 02 Jun 2002 03:00:00 GMT  
 Valid FileName
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.


Sat, 15 Jun 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. valid filenames for shell functions?

2. Valid Filenames?

3. Checking for valid filename

4. check string as valid filename

5. API to test for valid filename?

6. DOS wildcard, *, not valid with .FileName?

7. Detecting if a filename is a valid picture file

8. Converting an invalid filename to a valid one

9. getting just the filename, not the filename path from the opendialog control

10. SHORT filename to LONG filename???

11. ? How extract filename, dir, filename, extension ?

12. Short Filename to Long Filename ???

 

 
Powered by phpBB® Forum Software