Solved

Path is valid

Posted on 1998-10-23
15
236 Views
Last Modified: 2008-02-01
Need a function
IsPathValid(strPath as string) as boolean
  'returns true if strPath exist
  'false if not
end function

NB.
strPath may come as
1.c:\test\myfile.txt
2.c:\test
3.c:\test\

It has to be 100% bulletproof. It shuold return true if c:\test exist.
Please test before answering

perove
0
Comment
Question by:perove
  • 6
  • 4
  • 2
  • +3
15 Comments
 
LVL 1

Expert Comment

by:jow
ID: 1966517
I think this one should be >90% bulletproof.

~~~~~~~~~~~~~~~~~~~

Function IsPathValid(strPath As String) As Boolean
Dim a As Integer

If Dir(strPath & "\") <> "" Then
    IsPathValid = True
    Exit Function
End If

If Dir(strPath) <> "" Then
    IsPathValid = True
    Exit Function
End If
   
For a = Len(strPath) To 1 Step -1
    If Mid$(strPath, a, 1) = "\" Then
        If Mid$(strPath, a - 1, 1) <> ":" Then
        strPath = Left$(strPath, a)
        a = 0
    End If
    End If
Next

If Dir(strPath) <> "" Then
    IsPathValid = True
Else
    IsPathValid = False
End If
   
End Function

~~~~~~~~~~~~~~~~~~~

I don't know is there any improvement can be made, may be somebody can come out with a better solution.

Rgds Jow
0
 
LVL 9

Author Comment

by:perove
ID: 1966518
Nope,
Thanks for the effort but you came as far as I came.

If you have a folder c:\test that IS EMPTY (in other words conatins no documents,files etc) the function will fail.
So I'll re open the question
perove

0
 
LVL 3

Expert Comment

by:SPECIALIST
ID: 1966519
Very easy just use this

findfil = IIf(Dir$("C:AOL30") = "", False, True)

MsgBox (findfile)

Good luck!

Specialist

0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 9

Author Comment

by:perove
ID: 1966520
Pleas read the question, and test before answering:
IIf(Dir$("C:\po") = "", False, True)
gives me false even if c:\po is a directory that exist
(maby I'm missig somethig here but jow was closer)

IIf(Dir$("C:\po\") = "", False, True) gives me true if and only if this directory contain files (same as jow)

Please test before answering
Thanks
perove





0
 
LVL 3

Expert Comment

by:dapperry
ID: 1966521
Use jow's function except on the line that says:

If Dir(strPath) <> "" Then

put in:

If Dir(strPath,vbDirectory)<>"" Then

I tested it.  It works.

:) D Perry
0
 
LVL 3

Expert Comment

by:dapperry
ID: 1966522
On second thought - Do you need a function that will tell you whether a directory exists if dir is entered and whether a file exists if a directory/filename is entered? Or rather do you want the function to tell you whether the directory exists that holds the file if a directory/filename is entered?  Let me know, because if its the former you would need to determine whether the user entered a filename or just a directory.  If they entered a directory then you would use vbdirectory, but with directory/filename you would not use this option.  Let me know which way you want to go, and I'll post the whole code.

:) D Perry
0
 
LVL 9

Author Comment

by:perove
ID: 1966523
Well, we are gettin closer.
But if I try:
IsPathValid("c:\poi\uu.txt")

where c:\poi is a empty directory(but it exsists) then it return false, it should have been true.(case the PATH (for the directory) is valid.)

It's getting hotter..

perove



0
 
LVL 9

Author Comment

by:perove
ID: 1966524
Sorry, sorry, sorry
(we are cross posted here...)
I did just repace the first
If Dir(strPath) <> "" Then
if I replace both it does indeed work

To you latest comment:
I whant the function to return true if the folder is valid even if the file is not.
But hey it is working good enough for me now, proveide a new answer and you will be credit

perove
0
 
LVL 3

Expert Comment

by:dapperry
ID: 1966525
I'm writing up the new function now, but have one more question.  Filenames don't necessarily have to have extensions, for instance the file SERVICES that you find in c:\windows (at least if you use win95) So, if someone were to enter "c:\windows\services" would you want the function to look for the validity of "c:\windows\" or "c:\windows\services\".  Its hard to tell, because the user could really be looking for either.  Let me know how you would like to proceed.

:) D Perry
0
 
LVL 5

Expert Comment

by:tuvi
ID: 1966526
To add to dapperry's comment: that uu.txt can be a directory name too.
0
 

Expert Comment

by:Lantams56789
ID: 1966527
perove, please try this function.....

Function IsPathValid(strPath as string) As Boolean

         If Dir(strPath, vbDirectory) <> "" Then
                IsPathValid = True
         Else
                IsPathValid = False
         End if
     
End Function

Hopefully this would work.... :-)
0
 

Expert Comment

by:Lantams56789
ID: 1966528
ok, when calling IsPathValid function, the syntax would be:

     an example calling the function check if the path exist:

           Call IsPathValid(<argument here>)
           If IsPathValid Then
             Msgbox "The Path exist"
           Else
             Msgbox "The Path doesn't exist"
           end if

good luck
     
0
 
LVL 9

Author Comment

by:perove
ID: 1966529
Lantam
As you can see I've already got a answer from daperry.
Thanks anyway

daperry
prp. an answer please and you will have points

0
 
LVL 9

Author Comment

by:perove
ID: 1966530
See previous comment,
Thanks

0
 
LVL 3

Accepted Solution

by:
dapperry earned 30 total points
ID: 1966531
Ok - heres an answer.  Hopefully the function is as good as it needs to be now.  Thanks for the points.

:) D Perry
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What's wrong with CopyFromRecordset 6 33
Allow user to edit an outgoing email 2 24
Access on Mouse move 5 30
SQL Group on First occurrence 9 24
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

813 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now