Connecting Tech Pros Worldwide Forums | Help | Site Map

Trimming a path string

ColinWard
Guest
 
Posts: n/a
#1: Nov 13 '05

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!

Pieter Linden
Guest
 
Posts: n/a
#2: Nov 13 '05

re: Trimming a path string


ColinWard <jetfighter3@hotmail.com> wrote in message news:<4148962a$0$26148$c397aba@news.newsgroups.ws> ...[color=blue]
> 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
>[/color]
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
Sunil Korah
Guest
 
Posts: n/a
#3: Nov 13 '05

re: Trimming a path string


ColinWard <jetfighter3@hotmail.com> wrote in message news:<4148962a$0$26148$c397aba@news.newsgroups.ws> ...[color=blue]
> 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![/color]

Colin,

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

HTH

Sunil Korah
G.J. v.d. Kamp
Guest
 
Posts: n/a
#4: Nov 13 '05

re: Trimming a path string


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 <jetfighter3@hotmail.com> wrote in message news:<4148962a$0$26148$c397aba@news.newsgroups.ws> ...[color=blue]
> 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![/color]
ColinWard
Guest
 
Posts: n/a
#5: Nov 13 '05

re: Trimming a path string


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!
Albert D. Kallal
Guest
 
Posts: n/a
#6: Nov 13 '05

re: Trimming a path string


"Pieter Linden" <pietlinden@hotmail.com> wrote in message
news:bf31e41b.0409152024.465f8823@posting.google.c om...
[color=blue][color=green]
>>[/color]
> I'm sure there's an easier way, but here's one...[/color]
[color=blue]
> strTemp = StrReverse(strFileName)[/color]

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
pleaseNOOSpamKallal@msn.com
http://www.attcanada.net/~kallal.msn


Pieter Linden
Guest
 
Posts: n/a
#7: Nov 13 '05

re: Trimming a path string


"Albert D. Kallal" <PleaseNOOOsPAMMkallal@msn.com> wrote in message news:<TdI2d.454703$gE.238453@pd7tw3no>...[color=blue]
> "Pieter Linden" <pietlinden@hotmail.com> wrote in message
> news:bf31e41b.0409152024.465f8823@posting.google.c om...
>[color=green][color=darkred]
> >>[/color]
> > I'm sure there's an easier way, but here's one...[/color]
>[color=green]
> > strTemp = StrReverse(strFileName)[/color]
>
> 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)[/color]


See, I TOLD YA there had to be an easier way!!! Thanks Albert!
Albert D. Kallal
Guest
 
Posts: n/a
#8: Nov 13 '05

re: Trimming a path string


"Pieter Linden" <pietlinden@hotmail.com> wrote in message
news:bf31e41b.0409171852.3731276@posting.google.co m...
[color=blue]
>
> See, I TOLD YA there had to be an easier way!!! Thanks Albert![/color]

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
pleaseNOOSpamKallal@msn.com
http://www.attcanada.net/~kallal.msn


Closed Thread