extracting a file name from a path and file name

P

Paul

Is there a way to use some combination of String functions to return the
file name from a complete file spec, when the different strings to be
evaluated have varying path lengths, such as

M:\Project\Accounting\file1.xls
M:\Project\RD\file2.xls

In other words, I'm in need of a function that would return file1.xls and
file2.xls from those strings.

Thanks in advance,

Paul
 
P

Paul

I found an answer to my own question after running a few more searches:

InStrRev(stringcheck, stringmatch[, start[, compare]])) is the function I
was looking for.

One unexpected thing I noticed about this function, however, is that while
it searches from the right, the number it returns is the character position
of the search string counting from the left. I puzzled over that for a few
minutes before I realized what it was doing. I would have expected it to
return the number of characters from the right, rather than from the left,
since it's counting from the right, so it seems counterintuitive to me.

No matter, though, because you can always use it with Len() to get the
characters on the right as well.

Paul
 
B

Bob Quintal

Is there a way to use some combination of String functions to
return the file name from a complete file spec, when the different
strings to be evaluated have varying path lengths, such as

M:\Project\Accounting\file1.xls
M:\Project\RD\file2.xls

In other words, I'm in need of a function that would return
file1.xls and file2.xls from those strings.

Thanks in advance,

Paul

Public Function fileFromPath(PathAndFile)
Dim x
x = Split(PathAndFile, "\")
fileFromPath = x(UBound(x))
End Function
 
D

Douglas J. Steele

Actually, you don't need to know the length.

Mid("M:\Project\Accounting\file1.xls",
InStrRev("M:\Project\Accounting\file1.xls", "\") + 1)

will give you file1.xls
 
P

Paul

Interesting solution, Bob. I wasn't familiar with the Split() and UBound()
functions. I'm going to experiment with them.

Thanks much.

Paul
 
P

Paul

Thanks for pointing out a better way to do it, Doug.

I was using

Right(strTemplateFile, Len(strTemplateFile) - InStrRev(strTemplateFile,
"\", -1))

It does the job, but your way is simpler and more straightforward.
 
J

Jack Leach

Split and UBound (and LBound and Join) are all functions for dealing with an
array.

The Split function splits a string into the elements of an array, by a
specified delimiter (in this case "\"). Ubound refers to the uppermost value
in the array elements.

There's are extremely handy for a number of things when you get to use them
(and rather ingenuitive for getting a filename... I've never seen them used
so).

Neat little function.

--
Jack Leach
www.tristatemachine.com

"I haven''t failed, I''ve found ten thousand ways that don''t work."
-Thomas Edison (1847-1931)



Paul said:
Interesting solution, Bob. I wasn't familiar with the Split() and UBound()
functions. I'm going to experiment with them.

Thanks much.

Paul
 
M

Mike Painter

Jack said:
Split and UBound (and LBound and Join) are all functions for dealing
with an array.

The Split function splits a string into the elements of an array, by a
specified delimiter (in this case "\"). Ubound refers to the
uppermost value in the array elements.

There's are extremely handy for a number of things when you get to
use them (and rather ingenuitive for getting a filename... I've never
seen them used so).

Neat little function.
Giving up the string handling functiond from Pick Basic was so hard.
 
J

Jack Leach

I'm not familiar with Pick Basic... did it have similar methods?

--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)
 
M

Mike Painter

Yes, and many more.
Pick uses multi-valued fields which have special delimiters and many of the
functions are built around this.
Even Trim is a bit different as it reduces all occureances of two or more
spaces to one space. Trim(" This is a string " ) becomes "This is
a string"

A soundex index in Pick basic is about 10 lines long with no loops.

Access 2007 includes multivalued fields which have a lot of value if you
know how to use them in a relational database.

I just hope that Access will i teh future do something that is exposed in
Pick.
The contents of the last record should be exposed in Access

This = ThisField.lastvalue would solve questions that comes up all the time
here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top