• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 179
  • Last Modified:

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".

0
leeann
Asked:
leeann
  • 3
  • 2
  • 2
  • +1
1 Solution
 
deightonCommented:
'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
 
deightonCommented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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
 
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
 
deightonCommented:
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now