Link to home
Start Free TrialLog in
Avatar of GeneM
GeneM

asked on

Need Function to Validate File/Dir Name

Hello Experts

I need a function to validate a fully qualified file name I send to it.

The function should return one of 3 values:
-  File name is valid
-  File name is not valid but the directory is valid
-  Invalid directory

I thought someone might have a function that does something like this.

This is for VB6.  The function must work in all Windows (Win 95 thru XP).

Thanks in advance.
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi GeneM,

Well you can do it like this:

Private Enum FilePathStatus
    FileAndPathOK = 0
    PathOK = 1
    FileAndPathBad = 2
End Enum

Private Sub Command1_Click()
    Select Case ValidatePathAndFile(Text1.Text)
    Case FileAndPathOK
        MsgBox "OK"
    Case PathOK
        MsgBox "Bad File"
    Case FileAndPathBad
        MsgBox "Completely stuffed"
    End Select
End Sub

Private Function ValidatePathAndFile(ByVal What As String) As FilePathStatus
    If Dir(What) <> "" Then
        ValidatePathAndFile = FileAndPathOK
    Else
        strPath = Left(What, InStr(What & "\", "\"))
        If GetAttr(strPath) And vbDirectory = vbDirectory Then
            ValidatePathAndFile = PathOK
        Else
            ValidatePathAndFile = FileAndPathBad
        End If
    End If
End Function


Tim Cottee MCSD, MCDBA, CPIM
http://www.timcottee.tk 

Brainbench MVP for Visual Basic
http://www.brainbench.com
ASKER CERTIFIED SOLUTION
Avatar of bobbit31
bobbit31
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
FYI
And don't forget error-trapping or the routine will fail if you put in a filename containing certain characters (like maybe / , [] % = |)

The key, as seen in the above comments, is to use the Dir (or Dir$) function to check the path.
Avatar of supunr
supunr

' return values
' 0 - both path and file name is invalid
' 1 - only the path is valid
' 2 - only the file is valid ???
' 3 - both path and filename is valid
Private Function CheckPathNFile(PathNFile As String) As Integer
    Dim LBSlash As Long
    Dim PathName As String
   
    LBSlash = InStrRev(PathNFile, "\")
   
    If (LBSlash > 0) Then
        PathName = Left(PathNFile, LBSlash)
    Else
        PathName = PathNFile
    End If
   
    CheckPathNFile = 0
    If (Dir(PathName) <> "") Then   ' path valid
        CheckPathNFile = CheckPathNFile Or 1
    End If
    If (Dir(PathNFile) <> "") Then ' file valid
        CheckPathNFile = CheckPathNFile Or 2
    End If
   
End Function

Good Luck!
Avatar of GeneM

ASKER

Hi all,

I am going with Bobbit's solution.

Tim, your solution wasn't quite correct.  In the section where you were checking the directory, you looked for the leftmost "\" instead of the rightmost.  But then you used the getattr function, which created an error if the dir was invalid.

Supunr's solution was very similar to Bobbits.

Thanks to all.

GeneM