Split string (Path)

terbs

Registered User.
Local time
Tomorrow, 10:10
Joined
May 15, 2007
Messages
38
Im looking to seperate the file name from a paths string.

I assume you use the Split() Function to achieve this??

Ive tried somethig basic like this;

Code:
msgbox Split("C:\Pictures\1.jpg","\")

But I get error "Run-Time Error 13; Type mismatch"

Basically I need a string with "1.jpg" only. Ive looked all over for this but nothing seemed to work for me.
 
terbs,

Haven't tested it, but ...

strWhole = "C:\Pictures\1.jpg"

FileName = Mid(strWhole, InStrRev(strWhole, "\") + 1)

Wayne
 
perfect.

thank you Wayne :)
 
terbs,

Glad to help, InStrRev isn't that intuitive, hard to search for.

See ya,
Wayne
 
And just as an instructional item - in order to use the SPLIT function you need to assign it to a variant because this will become a single dimension array.

So you would use:

Dim varSplit As Variant

varSplit = Split("C:\Pictures\1.jpg","\")

And then you would find it like:

Msgbox varSplit(2)

But you would need to find the text after the last \ first so you would likely need

Msgbox varSplit(UBound(varSplit))

Wayne's code is much more efficient for this purpose but the Split function can come in handy in other ways so it is good to know how to use it.
 
Hi Wayne -

Works great (used A97 workaround for InStrRev()

FileName = Mid(strWhole, InStrRev97(strWhole, "\") + 1)
? filename
1.jpg

FilePath = left(strWhole, InStrRev97(strWhole, "\"))
? filepath
C:\Pictures\

Best Wishes - Bob
 
Hi Bob,

Good to see you again!

That's the only code I've written in a week, too much documentation and
query/report work.

Wayne
 
yeah bob I was coming to the understanding that the split function was creating an array, thanks for clearing it up.
 

Users who are viewing this thread

Back
Top Bottom