473,387 Members | 3,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Splitting a filename out of a filepath


I have to create a view where the filename is seperated from the path
to the file. Examples of the data include:

m:\images\big\myimg.jpg
m:\images\medium\myimg.jpg
z:\media\images\highqual\myimg.jpg

Is there a function that will return the position in a string of the
last \ ? With this I can easily use substring or similar to pull and
push this stuff into the fields I want. If this function does not
exist how do I move forward?

Nov 23 '05 #1
5 3845
I don't think they have a CHARINDEX that searches from the end of the
string. If you want to know the last position of a character within a
string, you can abuse the reverse() function like this:

len(filepath) - charindex('/', reverse(filepath)) + 1

That gives you the position of the last '/' in the string. Note that
this only works if you're searching for a single character (that is, it
won't work if you replace the '/' with some arbitrary string with
length > 1). Also, if there is no '/' in the string, this guy will
return len(filename) + 1 as opposed to the normal charindex() which
returns 0 if the needle isn't found.

Nov 23 '05 #2
Thank you sir -- that should work nicely.

J

Nov 23 '05 #3
If you apply reverse to the first parameter in the charindex you could
search for multiple chars as well.

e.g.

declare
@search varchar(255),
@find varchar(255)

SELECT
@search = 'm:\images\big\myimg.jpg',
@find = '\'

SELECT
len(@search) - charindex(reverse(@find), reverse(@search)) + 1 ,
LEFT(@search, len(@search) - charindex(reverse(@find), reverse(@search)) +
1)

Returns
----------- -------------
14 m:\images\big\

declare
@search varchar(255),
@find varchar(255)

SELECT
@search = 'm:\images\big\myimg.jpg',
@find = 'images\'

SELECT
len(@search) - charindex(reverse(@find), reverse(@search)) + 1 ,
LEFT(@search, len(@search) - charindex(reverse(@find), reverse(@search)) +
1)

Returns

----------- --------
10 m:\images\
--
Terry Kreft

"ZeldorBlat" <ze********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
I don't think they have a CHARINDEX that searches from the end of the
string. If you want to know the last position of a character within a
string, you can abuse the reverse() function like this:

len(filepath) - charindex('/', reverse(filepath)) + 1

That gives you the position of the last '/' in the string. Note that
this only works if you're searching for a single character (that is, it
won't work if you replace the '/' with some arbitrary string with
length > 1). Also, if there is no '/' in the string, this guy will
return len(filename) + 1 as opposed to the normal charindex() which
returns 0 if the needle isn't found.

Nov 23 '05 #4
>If you apply reverse to the first parameter in the charindex you could
search for multiple chars as well.


Good point...I hadn't thought of that.

Nov 23 '05 #5
Wll, I didn't think of using reverse in the frst place so your still ahead
of me on this one <g>.

--
Terry Kreft

"ZeldorBlat" <ze********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
If you apply reverse to the first parameter in the charindex you could
search for multiple chars as well.


Good point...I hadn't thought of that.

Nov 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: mjakowlew | last post by:
Hi, I'm trying to use some string manipulation from a file's path. filepath='c:\documents\web\zope\file.ext' I need to extract everthing after the last '\' and save it. I've looked around...
3
by: Darrel | last post by:
using Request.FilePath will return the entire file path: directory/directory/myfile.aspx Is there a built function in ASP.net to grab just the filename (myfile.aspx) or do I need to write...
0
by: the_programming_paddy | last post by:
I'm using VWD Express 2005. Sometimes I'll have a number of pages open that are all related and make calls to each other at different times. As I'm going through the code of one, I'll come to a...
3
by: Ramon F Herrera | last post by:
My application receives a filepath as an argument. Obviously, it can come in several formats, such as: filename.ext ..\filename.ext ...\..\filename.ext this\is_a\relative\path\finally.ext
3
by: Hamayun Khan | last post by:
I have Two asp pages addfiles.asp insert.asp addfiles.asp is used to browse the files which i want to upload to server. in IE both the pages run and the files uploads ok. There is...
2
by: AdrianJ1980 | last post by:
Hi all, Apologies if this has been discussed before, but my numerous searches (google and otherwise) have come up with nothing. What i need to do is open a word document (from Access 2003),...
0
by: rajib123 | last post by:
I am trying to write a windows utility that downloads some files from an ftp site to a local directory and then uploads those files to another ftp site. The files that I need to download are posted...
2
by: indona | last post by:
hi.. i am trying to pass the file path of a text file, which i need to read, to a file object so as to read all lines of the text file. and i am selecting the file through a filedialog and i...
3
by: Chuck Beck | last post by:
I am struggling with this issue right now. I have read in a number of places that you should not store the image itself in the database, but rather only the filepath/filename. I can't seem to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.