A Dir Function Problem

How can we get the directory from the pathname(including file name) given by a user? For example: "c:\Junk\myFile.txt", and I only want the directory "c:\Junk".

leeannAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

deightonprogCommented:
'example
         sclean = "c:\data\cg.exe"
         
         
              sdeighton = ""
              For andy = Len(sclean) To 1 Step -1
               
                If Mid(sclean, andy, 1) = "\" Then Exit For
               
                sdeighton = Mid(sclean, andy, 1) & sdeighton
               
              Next
              MsgBox sdeighton
0
deightonprogCommented:
Sorry you wanted the path, that gives you the file.

Private Sub Command1_Click()

    'example
         sclean = "c:\data\cg.exe"
         
         
              sdeighton = ""
              For andy = Len(sclean) To 1 Step -1
               
                If Mid(sclean, andy, 1) = "\" Then Exit For
               
                sdeighton = Mid(sclean, andy, 1) & sdeighton
               
              Next
              MsgBox sdeighton
              spath = Left(sclean, Len(sclean) - Len(sdeighton) - 1)
              MsgBox spath
             

End Sub
0
leeannAuthor Commented:
But the problem is that the pathname the user input may or may not followed by the a file name.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

moterkCommented:
If the file or path exists you can do the following:

Store path or filename in fpathname$

dotloc%=instr(1,fpathname$,".")

if dotloc%=0 then
    'no dot likely a Path
    'do nothing or add slash \ to
    'end if not already there
else
    'filename
    Fileonly$=dir(fpathname$)
    flen%=len(fpathname$)-len(Fileonly$)
    fpathname$=left(fpathname$,flen%)
end if

Also insted of looking for a . since some paths may have a . (not a good idea) you can use the GetAttrib function to determine if it is a file or a path    
0
rondeaujCommented:
This will check for both directories and files plus a little extra for a Novel bug if you are checking across a network.


Public Function FileExists(fname As String) As Boolean
' return true if file exists
Dim s As String

 'allow for directories too, to make the function more versatile
On Error GoTo DirErr
s = Dir(fname, vbNormal + vbDirectory)
On Error Resume Next
If s = "" Then
    FileExists = False
Else
    FileExists = True
    ' finish out the dir for Novell bug
    Do While Dir() <> ""
    Loop
End If


Exit Function
DirErr:
    FileExists = False
End Function
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rondeaujCommented:
Sorry I read your question wrong this is what you need:

Public Function ExtractPath(s As String) As String
' given a filespec, extract the path portion

Dim fileext As String
Dim ret As Integer

' extract the path
' use the last ":" or "\" in the string
fileext = s
ret = Len(fileext)

Do While ret > 0
    If Mid$(fileext, ret, 1) = ":" Or Mid$(fileext, ret, 1) = "\" Then
        Exit Do
    End If
    ret = ret - 1
Loop

If ret > 0 Then
    fileext = Left$(fileext, ret)
    If Right$(fileext, 1) = "\" And Right$(fileext, 2) <> ":\" Then
        fileext = Left$(fileext, Len(fileext) - 1)
    End If
    ExtractPath = fileext
Else
    ExtractPath = ""
End If

GetFileExt_VBRigErr:
 End Function
0
leeannAuthor Commented:
I think this point should give Moterk. Any way, thanks a lot!
0
deightonprogCommented:
To leann

you rejected my answer with

>>>But the problem is that the pathname the user input may or may not followed by the a file name.

but in your question you wrote

>>>>How can we get the directory from the pathname(including file name)

People go to effort to answer these questions you know!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.