Returning directory name from filepath?

PaulSpell

Registered User.
Local time
Today, 16:21
Joined
Apr 19, 2002
Messages
201
Can someone please tell me how to return the path to a directory, from a full file path?

Basically I want to use the filepath returned by a "file open" function to convert to a path to the directory, which I can then use as an argument in TransferDatabase.
 
This should show you how to do it...

Private Function ParseFileName()

Dim sFullName As String
Dim sFilePathOnly As String
Dim sDrive As String
Dim sPath As String
Dim sLocation As String
Dim sFileName As String

sFullName = tbFileInfo.Value
'sFullName = CurrentDb.Name

' Find the final "\" in the path.
sPath = sFullName

Do While Right$(sPath, 1) <> "\"
sPath = Left$(sPath, Len(sPath) - 1)
Loop

' Find the Drive.
sDrive = Left$(sFullName, InStr(sFullName, ":") + 1)
tbDrive = sDrive

' Find the Location.
sLocation = Mid$(sPath, Len(sDrive) - 2)
tbLocation = sLocation

' Find the Path.
sPath = Mid$(sPath, Len(sDrive) + 1)
tbPath = sPath

' Find the file name.
sFileName = Mid$(sFullName, Len(sPath) + 4)
tbFileName = sFileName

End Function

'HTH
 
Ah yes thanks, this is the type of thing I have done in the past. Presumably there is no alternative then?
 
How 'bout this one?

Code:
Function GetPath(strPath As String) As String
    GetPath = Left(strPath, Len(strPath) - Len(Dir(strPath)))
End Function
 
I have a function that thorugh API which calls the 'browse for folder' dialog and returns the directory. If you'd like a copy just ask.
 
Tim,

It might be too early in the morning for this but I keep returning the actual string (file path and file name) with your function instead of just the path.

Public Function GetFilePath(strPath As String) As String
GetFilePath = Left(strPath, Len(strPath) - Len(Dir(strPath)))
End Function

Function Test()
Dim s As String
s = "C:\My Documents\Databases\Monthly\Taxes.txt"
MsgBox Left(s, Len(s) - Len(Dir(s)))
End Function

Thanks!
 

Users who are viewing this thread

Back
Top Bottom