?
Solved

Need Function to Validate File/Dir Name

Posted on 2003-03-10
5
Medium Priority
?
309 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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
Course of the Month9 days, 21 hours left to enroll

762 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