Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Truncating string

Former Member
0 Likes
1,428

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

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,390


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

13 REPLIES 13
Read only

Former Member
0 Likes
1,390

use <b>SPLIT</b> Statement .....

else Use FM <b>TEXT_SPLIT</b>

Read only

0 Likes
1,390

split by "/"? but how can I get rid of the ".txt" then if the file name can be of any length? Thanks!

Read only

0 Likes
1,390

hi anyi,

check this fm <b>'PC_SPLIT_COMPLETE_FILENAME'</b>

Read only

Former Member
0 Likes
1,390

use FM SO_SPLIT_FILE_AND_PATH

br

Andrzej

Read only

Former Member
0 Likes
1,390

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

Read only

0 Likes
1,390

HI Use FM <b>TEXT_SPLIT</b>

Have a look at this link so as to know how to use that FM

You can check this so as to know as how to use Split statement

<b>http://help.sap.com/saphelp_erp2005/helpdata/en/fc/eb33f3358411d1829f0000e829fbfe/frameset.htm</b>

Regards,

Santosh

Read only

Former Member
0 Likes
1,390

Hi Anyi,

Use SPLIT <string> AT <delimiter> INTO TABLE <itab>.

Regards,

Laxmi.

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,391


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

Read only

0 Likes
1,390

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!

Read only

0 Likes
1,390

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

Read only

0 Likes
1,390

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

Read only

0 Likes
1,390

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

Read only

Former Member
0 Likes
1,390

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