Link to home
Start Free TrialLog in
Avatar of isnoend2001
isnoend2001Flag for United States of America

asked on

Add the last Directory File name from a path to a combobox

Typical Paths
C:\Documents and Settings\All Users.WINDOWS\Documents\RoofCalculator\Williams 1247 RiverBend rd\Roofing Bids
C:\Documents and Settings\All Users.WINDOWS\Documents\RoofCalculator\Williams 1247 RiverBend rd\Fencing Bids
C:\Documents and Settings\All Users.WINDOWS\Documents\RoofCalculator\Williams 1247 RiverBend rd\LanndScaping Bids
C:\Documents and Settings\All Users.WINDOWS\Documents\RoofCalculator\Williams 1247 RiverBend rd\Gardner Bids
C:\Documents and Settings\All Users.WINDOWS\Documents\RoofCalculator\Williams 1247 RiverBend rd\New Gutter Bids
Add to Combo box
With cboBids
.AddItem Roofing Bids
.AddItem Fencing Bids
.AddItem LanndScaping Bids
.AddItem Gardner Bids
.AddItem New Gutter Bids
End With
How to do this ?
Avatar of jkaios
jkaios
Flag of Marshall Islands image

Isn't this the same as https://www.experts-exchange.com/questions/28517766/Loop-thru-a-directory-put-filenames-in-a-combobox-vb6.html?

Or do you mean JUST the LAST directory, which is "New Gutter Bids" and discard the other?
Avatar of isnoend2001

ASKER

Thanks for your reply I have decided not to use the Dir function
My app creates the directories so i have decided to store the files/paths in an array and save the array.
i have tried this:
Private Sub Command5_Click()
Dim Result As String
Result = LastDirctory("C:\Documents and Settings\All Users.WINDOWS\Documents\RoofCalculator\Williams 1247 RiverBend rd\New Gutter Bids")
Debug.Print Result
End Sub

Function LastDirctory(FileWithPath As String) As String
Dim BackSlash As String
Dim position As Integer
BackSlash = "\"
position = InStrRev(FileWithPath, BackSlash, , vbTextCompare)
LastDirctory = Right(FileWithPath, position)
End Function

Open in new window

But this returns this:
d Settings\All Users.WINDOWS\Documents\RoofCalculator\Williams 1247 RiverBend rd\New Gutter Bids
I only want to return: New Gutter Bids
what is wrong ?
SOLUTION
Avatar of jkaios
jkaios
Flag of Marshall Islands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for adding that
I get Bad Filename or number on this line:
MyName = Dir(MyPath, vbDirectory)   'Retrieve the first entry.
I got this to work:
Function LastDirctory(FileWithPath As String) As String
    Dim FilewithPathLength As Integer
    Dim BackSlash As String
    Dim position As Integer
    FilewithPathLength = Len(FileWithPath)
    BackSlash = "\"
    position = InStrRev(FileWithPath, BackSlash, , vbTextCompare)
    position = FilewithPathLength - position
    LastDirctory = Right(FileWithPath, position)
End Function
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks