Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# Path is valid

Posted on 1998-10-23
Medium Priority
240 Views
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.

perove
0
Question by:perove
• 6
• 4
• 2
• +3

LVL 1

Expert Comment

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

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

ID: 1966519
Very easy just use this

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

MsgBox (findfile)

Good luck!

Specialist

0

LVL 9

Author Comment

ID: 1966520
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)

Thanks
perove

0

LVL 3

Expert Comment

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

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

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

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

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

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

Expert Comment

ID: 1966527

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

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

ID: 1966529
Lantam
Thanks anyway

daperry

0

LVL 9

Author Comment

ID: 1966530
See previous comment,
Thanks

0

LVL 3

Accepted Solution

dapperry earned 120 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

Question has a verified solution.

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

Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as formâ€¦
###### Suggested Courses
Course of the Month12 days, 17 hours left to enroll