cancel
Showing results for 
Search instead for 
Did you mean: 

Parse file name and path from full path

2,075

This is how I do it today, but I suspect there are better ways to do it?

BEGIN

declare ls_path nvarchar(200);

set ls_path = 'C:\\tempfolder\\test\\testfile.txt';

select 
    left(ls_path, len(ls_path) -len(row_value) ) cc_path, row_value cc_filename
from 
    sa_split_list(ls_path, '\\')
where 
    row_value <> '' and
    line_num = (select max(line_num) from sa_split_list(ls_path, '\\') );

END

Accepted Solutions (1)

Accepted Solutions (1)

MarkCulp
Participant

I would think it is easier to use locate( ..., '\\\\', -1 ) to find the last backslash and then split the string at that location.

begin
  declare @path varchar(255) = 'c:\\\\my\\\\path\\\\name\\\\filename.jpg';
  select locate( @path, '\\\\', -1 ) as len,
         left( @path, len-1 ) as path,
         substr( @path, len+1 ) as filename;
end;

Note that backslashes should be doubled ... otherwise sequences like \\n will get interpreted as newline (similar for \\r and \\t and a few others)

HTH

VolkerBarth
Contributor
0 Kudos

Wow, was just about to post something comparable but had to lookup the third LOCATE() parameter, as usual:)

Answers (0)