Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract name of a sub directory from directory path 1

Status
Not open for further replies.

thomsons

Programmer
Dec 16, 2002
19
0
0
GB
I'm trying to extract the name of a sub directory from a directory path.

Path - c:\thomsons\stuff\exciting.xls

Using the following code (picked up from Tek Tips by Supra - cheers) I'm able to extract the full directory path and also the filename but what I am wanting to do is extract the sub directory 'Stuff'


Private Sub Form_Open()

Dim FullPath As String
Dim FileName As String
Dim FileDir As String

FullPath = "c:\thomsons\stuff\exciting.xls"
FileName = FileFromPath(FullPath)
FileDir = DirFromPath(FullPath)

MsgBox "Full Path: " & FullPath & vbCrLf & _
"File Name: " & FileName & vbCrLf & _
"Directory: " & FileDir

End Sub

Private Function DirFromPath(strPath As String)

Dim Pos As Long

Pos = InStrRev(strPath, "\")
DirFromPath = Left(strPath, Pos)

End Function

Private Function FileFromPath(strPath As String)

Dim Pos As Long

Pos = InStrRev(strPath, "\")
FileFromPath = Right(strPath, Len(strPath) - Pos)

End Function

Cheers,
ST
 
Modify the DirFromPath function to:

DirFromPath = Left(strPath, Pos-1)

to leave off the final "\". Now you can use

FileFromPath(DirFromPath(fullname)) to return the subdirectory name.
Rob
[flowerface]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top