By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 795 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

Trimming a path string

P: n/a

Hello.

I use the following line which returns the path of a file and adds it to
a listbox. How can I modify this line so that it only returns the
filename and not the entire path?
--> TxtAttachment.AddItem (vrtSelectedItem)
vrtSelectedItem is the variable which contains the path.

Thank you

Colin


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
ColinWard <je*********@hotmail.com> wrote in message news:<41**********************@news.newsgroups.ws> ...
Hello.

I use the following line which returns the path of a file and adds it to
a listbox. How can I modify this line so that it only returns the
filename and not the entire path?
--> TxtAttachment.AddItem (vrtSelectedItem)
vrtSelectedItem is the variable which contains the path.

Thank you

Colin

I'm sure there's an easier way, but here's one...

Option Compare Database
Option Explicit

Public Function PathOnly(ByVal strFileName As String) As String
Dim strTemp As String
strTemp = StrReverse(strFileName)
strTemp = Left$(strTemp, InStr(1, strTemp, "\") - 1)
strTemp = StrReverse(strTemp)
PathOnly = strTemp
End Function
Nov 13 '05 #2

P: n/a
ColinWard <je*********@hotmail.com> wrote in message news:<41**********************@news.newsgroups.ws> ...
Hello.

I use the following line which returns the path of a file and adds it to
a listbox. How can I modify this line so that it only returns the
filename and not the entire path?
--> TxtAttachment.AddItem (vrtSelectedItem)
vrtSelectedItem is the variable which contains the path.

Thank you

Colin


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Colin,

Try
--> TxtAttachment.AddItem (mid(vrtSelectedItem,InStr(vrtSelectedItem,"\")+1) )

HTH

Sunil Korah
Nov 13 '05 #3

P: n/a
Colin,

First, find the position of the last slash in the string, everything
after that is the filename:

Function GetFile(inPathFile As String) As String

Dim iLastSlash As Integer

Do Until (InStr(iLastSlash + 1, inPathFile, "\")) = 0
iLastSlash = InStr(iLastSlash + 1, inPathFile, "\")
Loop

GetFile = Right(inPathFile, Len(inPathFile) - iLastSlash)

End Function

Regards, GJ
ColinWard <je*********@hotmail.com> wrote in message news:<41**********************@news.newsgroups.ws> ...
Hello.

I use the following line which returns the path of a file and adds it to
a listbox. How can I modify this line so that it only returns the
filename and not the entire path?
--> TxtAttachment.AddItem (vrtSelectedItem)
vrtSelectedItem is the variable which contains the path.

Thank you

Colin


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 13 '05 #4

P: n/a
Thank you guys.

I tried sunil's solution first because it looked simplest to implement.
Unfortunately, all it did was strip the drive letter off the path. I
then went to pieter's solution and it worked perfectly.

Thank you all

Colin

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #5

P: n/a
"Pieter Linden" <pi********@hotmail.com> wrote in message
news:bf**************************@posting.google.c om...
I'm sure there's an easier way, but here's one...

strTemp = StrReverse(strFileName)


Gee, I was NOT aware of the "reverse" function, and thus your solton is
cute!!

However, do note that we have

InstrRev, which is simply Instr, but it stards at the rigth side...

So...

FileNameOnly = mid$(strFileName , InStrRev(strFileName, "\") + 1)

--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pl*****************@msn.com
http://www.attcanada.net/~kallal.msn
Nov 13 '05 #6

P: n/a
"Albert D. Kallal" <Pl*******************@msn.com> wrote in message news:<TdI2d.454703$gE.238453@pd7tw3no>...
"Pieter Linden" <pi********@hotmail.com> wrote in message
news:bf**************************@posting.google.c om...

I'm sure there's an easier way, but here's one...

strTemp = StrReverse(strFileName)


Gee, I was NOT aware of the "reverse" function, and thus your solton is
cute!!

However, do note that we have

InstrRev, which is simply Instr, but it stards at the rigth side...

So...

FileNameOnly = mid$(strFileName , InStrRev(strFileName, "\") + 1)

See, I TOLD YA there had to be an easier way!!! Thanks Albert!
Nov 13 '05 #7

P: n/a
"Pieter Linden" <pi********@hotmail.com> wrote in message
news:bf*************************@posting.google.co m...

See, I TOLD YA there had to be an easier way!!! Thanks Albert!


You are welcome. Fact is...we both learned some new things here..and that is
a great part of helping out in newsgroups...
--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pl*****************@msn.com
http://www.attcanada.net/~kallal.msn
Nov 13 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.