Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Need Function to Validate File/Dir Name

Posted on 2003-03-10
5
Medium Priority
?
319 Views
Last Modified: 2012-05-04
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.
0
Comment
Question by:GeneM
5 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 8103589
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
0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 400 total points
ID: 8103598
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)
   Else
       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"
   Else
       checkFile = "Invalid Directory"
   End If
   
End Function
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 8104145
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.
0
 
LVL 11

Expert Comment

by:supunr
ID: 8106881
' 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!
0
 
LVL 3

Author Comment

by:GeneM
ID: 8107348
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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question