‎2006 Jun 26 7:54 PM
Hello,
Say for example I have a string of file directory in such a way:
c:/dir/subdir/subdir/subdir/.../filename.txt
and i would like to extract only filename from this string, how should I do it? Thanks a lot!
Anyi
‎2006 Jun 26 8:09 PM
report zrich_0001.
parameters: fullname type rlgrap-filename
default 'c:/dir/subdir/subdir/subdir/.../filename.txt'.
data: file_name type rlgrap-filename.
data: file_path type rlgrap-filename.
call function 'SO_SPLIT_FILE_AND_PATH'
exporting
full_name = fullname
importing
stripped_name = file_name
file_path = file_path.
write:/ file_name.
Regards,
Rich Heilman
‎2006 Jun 26 8:00 PM
use <b>SPLIT</b> Statement .....
else Use FM <b>TEXT_SPLIT</b>
‎2006 Jun 26 8:02 PM
split by "/"? but how can I get rid of the ".txt" then if the file name can be of any length? Thanks!
‎2006 Jun 26 8:09 PM
‎2006 Jun 26 8:03 PM
‎2006 Jun 26 8:04 PM
Look at this code below.
DATA: v_index LIKE sy-tabix.
DO.
SEARCH p_file+v_index FOR '/'. " or '' depending on unix or pc.
IF sy-subrc <> 0.
v_file_name = p_file+v_index.
ELSE.
v_index = v_index + sy-fdpos + 1.
ENDIF.
ENDDO.I didn't see the response you gave regarding the extension part of it. v_file_name from my code above will give you the file name with the extension. You can then split the file name at '.' and get file name and extension separated out.
Message was edited by: Srinivas Adavi
‎2006 Jun 26 8:05 PM
‎2006 Jun 26 8:08 PM
Hi Anyi,
Use SPLIT <string> AT <delimiter> INTO TABLE <itab>.
Regards,
Laxmi.
‎2006 Jun 26 8:09 PM
report zrich_0001.
parameters: fullname type rlgrap-filename
default 'c:/dir/subdir/subdir/subdir/.../filename.txt'.
data: file_name type rlgrap-filename.
data: file_path type rlgrap-filename.
call function 'SO_SPLIT_FILE_AND_PATH'
exporting
full_name = fullname
importing
stripped_name = file_name
file_path = file_path.
write:/ file_name.
Regards,
Rich Heilman
‎2006 Jun 26 8:29 PM
I kind of like Rich's answer, but his answer does not truncate the extension of the file as well, any tip on that, the file could be of any extension with variable length. Thanks!
‎2006 Jun 26 8:34 PM
Oh, must have missed that part.
report zrich_0001.
parameters: fullname type rlgrap-filename
default 'c:/dir/subdir/subdir/subdir/.../filename.txt'.
data: file_name type rlgrap-filename.
data: file_path type rlgrap-filename.
<b>data: file_ext type rlgrap-filename.</b>
call function 'SO_SPLIT_FILE_AND_PATH'
exporting
full_name = fullname
importing
stripped_name = file_name
file_path = file_path.
<b>split file_name at '.' into file_name file_ext.</b>
write:/ file_name.
Regards,
Rich Heilman
‎2006 Jun 26 8:45 PM
Since we now that the extension will always be a length of three and a period, we can do this too.
report zrich_0001.
parameters: fullname type rlgrap-filename
default 'c:/dir/subdir/subdir/subdir/.../filename.txt'.
data: file_name type rlgrap-filename.
data: file_path type rlgrap-filename.
call function 'SO_SPLIT_FILE_AND_PATH'
exporting
full_name = fullname
importing
stripped_name = file_name
file_path = file_path.
<b>shift file_name right deleting trailing space.
shift file_name right by 4 places.
shift file_name left deleting leading space.</b>
write:/ file_name.
Regards,
Rich Heilman
‎2006 Jun 26 9:15 PM
You could even do this.
report zrich_0001.
parameters: fullname type rlgrap-filename
default 'c:/dir/subdir/subdir/subdir/.../filename.txt'.
data: file_name type rlgrap-filename.
data: file_path type rlgrap-filename.
call function 'SO_SPLIT_FILE_AND_PATH'
exporting
full_name = fullname
importing
stripped_name = file_name
file_path = file_path.
<b>search file_name for '...'.
if sy-subrc = 0.
clear file_name+sy-fdpos(4).
endif.</b>
write:/ file_name.
Regards,
Rich Heilman
‎2006 Jun 26 10:01 PM
Hi Anyi,
try this one.
data: file_path type string ,
file_name type string ,
file_ext type string ,
n type i.
data: begin of itab occurs 0,
str(25) type c,
end of itab.
file_path = 'C:/Test/Text.txt'.
split file_path at '/' into table itab.
loop at itab.
file_name = itab-str.
endloop.
split file_path at '.' into file_name file_ext.
write file_name.
hope this helps