leeann
asked on
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".
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
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
ASKER
But the problem is that the pathname the user input may or may not followed by the a file name.
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
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(
fpathname$=left(fpathname$
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
I think this point should give Moterk. Any way, thanks a lot!
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!!
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!!
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