vba in excel
Author
Discussion

ProPlus

Original Poster:

3,810 posts

270 months

Saturday 9th September 2006
quotequote all
getopenfile name retrieves C:\folder1\test.txt, I just want to retrieve test.txt. How do i do this?

atomicrex

862 posts

257 months

Sunday 10th September 2006
quotequote all
ProPlus said:
getopenfile name retrieves C:folder1 est.txt, I just want to retrieve test.txt. How do i do this?


const csPath as string=C:folder1\test.txt
const csFind as string="\"


Public Sub TestPath()
Dim sFileName1 As String, sFileName2 As String
sFileName1 = GetFileName1(csPath)
sFileName2 = GetFileName2(csPath)
End Sub

Public Function GetFileName1(sPath As String) As String
Dim lOPos As Long, lNPos As Long

lNPos = 0
Do
lOPos = lNPos + 1
lNPos = InStr(lOPos, sPath, csFind)
If lNPos = 0 Then GetFileName1 = Mid(sPath, lOPos)
Loop Until lNPos = 0
End Function

Public Function GetFileName2(sPath As String) As String
GetFileName2 = Mid(sPath, InStrRev(sPath, csFind) + 1)
End Function

Edited by atomicrex on Sunday 10th September 08:45

ProPlus

Original Poster:

3,810 posts

270 months

Sunday 10th September 2006
quotequote all
atomicrex said:
ProPlus said:
getopenfile name retrieves C:folder1 est.txt, I just want to retrieve test.txt. How do i do this?


const csPath as string=C:folder1 est.txt
const csFind as string=""


Public Sub TestPath()
Dim sFileName1 As String, sFileName2 As String
sFileName1 = GetFileName1(csPath)
sFileName2 = GetFileName2(csPath)
End Sub

Public Function GetFileName1(sPath As String) As String
Dim lOPos As Long, lNPos As Long

lNPos = 0
Do
lOPos = lNPos + 1
lNPos = InStr(lOPos, sPath, csFind)
If lNPos = 0 Then GetFileName1 = Mid(sPath, lOPos)
Loop Until lNPos = 0
End Function

Public Function GetFileName2(sPath As String) As String
GetFileName2 = Mid(sPath, InStrRev(sPath, csFind) + 1)
End Function

Edited by atomicrex on Sunday 10th September 08:45


much appreciated.... was driving me crazy, in JAVA its so easy but using this crap programming language i am so limited..

roadsweeper

3,789 posts

304 months

Sunday 10th September 2006
quotequote all
ProPlus, I emailed this solution to you yesterday (except using InStrRev) as PH wasn't allowing me to post it. Did you not receive it?

ProPlus

Original Poster:

3,810 posts

270 months

Sunday 10th September 2006
quotequote all
roadsweeper said:
ProPlus, I emailed this solution to you yesterday (except using InStrRev) as PH wasn't allowing me to post it. Did you not receive it?


in the end i used:

FName = Mid(FName, InStrRev(FName, csFind) + 1)

where csfind is
and fname is complete file path obtained via getopenfilename

and it works fine....

cheers.

AtomicRex

862 posts

257 months

Monday 11th September 2006
quotequote all
Its a bugger getting these "\" to show!

I gave you both....as it depends on the version of VBA you are using. I have to program in the cutting edge of office 97! And in that they have only just realised that water flows down hill....so asking for InstRev is way too much to ask for!

Plus if you need to get something else out of the string its always useful to keep the code using the loop.

Glad it helped

roadsweeper

3,789 posts

304 months

Monday 11th September 2006
quotequote all
AtomicRex said:
Its a bugger getting these "" to show!

Aye, I gave up pretty quickly! hehe

AtomicRex said:
I have to program in the cutting edge of office 97!

My God man, you must have the patience of a saint! bow