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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimCotteeHead of Software ServicesCommented:
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
        strPath = Left(What, InStr(What & "\", "\"))
        If GetAttr(strPath) And vbDirectory = vbDirectory Then
            ValidatePathAndFile = PathOK
            ValidatePathAndFile = FileAndPathBad
        End If
    End If
End Function


Brainbench MVP for Visual Basic
try this:

Private Sub Command1_Click()
   MsgBox checkFile("C:\mydocuments\test.txt")
End Sub

Function checkFile(strFile As String) As String

   '' get the directory and file name
   Dim strF As String
   Dim strD As String
   Dim ix As Integer
   ix = InStrRev(strFile, "\")

   If (ix > 0) Then '' a valid path was given
        strD = Left(strFile, ix)
        strF = Right(strFile, Len(strFile) - ix)
       checkFile = "Invalid Directory"
   End If

   If Dir(strFile) <> "" Then '' then the file exists
       checkFile = "File Exists"
   ElseIf Dir(strD, vbDirectory) <> "" Then '' directory exists
       checkFile = "File does not exists, but is a valid directory"
       checkFile = "Invalid Directory"
   End If
End Function

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
' 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)
        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!
GeneMAuthor Commented:
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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.