Solved

Path is valid

Posted on 1998-10-23
15
237 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
[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
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

734 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