Application Development 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: 

split a string at the last '\'

Former Member
0 Kudos

Hi experts,

i have a string like str_a = 'C:\Documents and Settings\abc.def\asdfgh\abcde\filename.csv' and i want to split this string at the last backslash into

str_1 = 'C:\Documents and Settings\abc.def\asdfgh\abcde\' and

str_2 = 'filename.csv'

but i don't know how.

pls help and thanks a lot!

12 REPLIES 12

Former Member
0 Kudos

Hi,

Use Split statement.

DATA: str1 TYPE string,

str2 TYPE string,

str3 TYPE string,

itab TYPE TABLE OF string,

text TYPE string.

text = `What a drag it is getting old`.

SPLIT text AT space INTO: str1 str2 str3,

TABLE itab.

Thank U,

Jay....

Former Member
0 Kudos

Hi,

use this FM, U'll get filename

CALL FUNCTION 'SO_SPLIT_FILE_AND_PATH'

EXPORTING

full_name = p_fpath

IMPORTING

stripped_name = v_filename1

  • FILE_PATH =

EXCEPTIONS

X_ERROR = 1

OTHERS = 2

.

IF sy-subrc <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ELSE.

SPLIT v_filename1 AT '.' INTO v_1 v_2.

CLEAR v_filename1.

MOVE v_1 TO v_filename1.

ENDIF.

Former Member
0 Kudos

Plz ignore

Former Member
0 Kudos

Hi Ali


DATA : itab TYPE TABLE OF string.

SPLIT str_a AT '\' INTO TABLE itab.

LOOP AT itab.

AT LAST.
CONCATENATE itab str_1 '\' INTO str_1 
str_2 = itab.
EXIT.
ENDAT.

CONCATENATE itab str_1 INTO str_1 SEPARATED BY '\'.
AT FIRST.
SHIFT str_1 LEFT.
ENDAT.

ENDLOOP.

Pushpraj

Former Member
0 Kudos

Plz ignore

Former Member
0 Kudos

Hi Ali


DATA : itab TYPE TABLE OF string.

SPLIT str_a AT '\' INTO TABLE itab.

LOOP AT itab.

AT LAST.
CONCATENATE str_1 '\' INTO str_1.
str_2 = itab.
EXIT.
ENDAT.

CONCATENATE itab str_1 INTO str_1.
AT FIRST.
SHIFT str_1 LEFT.
ENDAT.

ENDLOOP.

Pushpraj

Former Member
0 Kudos

Hi,

Below is the Fm to split the filename:

  • Function module to split the file name

CALL FUNCTION 'CH_SPLIT_FILENAME'

EXPORTING

complete_filename = lv_ifile

IMPORTING

drive = lv_drive

extension = gv_ifile_ext

name = lv_ofile_name

path = lv_path

EXCEPTIONS

invalid_drive = 1

invalid_path = 2

OTHERS = 3.

You will have the path as required and name will hold the filename(splitt at last\) and then fileextension will also be stored in separrate variables

Regards

Shiva

Former Member
0 Kudos

Hi,

Use the below code.

I checked it, its working for your requirement.

DATA: w_fiel1 TYPE string,
      w_fiel2 TYPE string,
      w_off TYPE i,
      w_len1 TYPE i,
      w_len2 TYPE i.

w_fiel1 = 'C:\Documents and Settings\abc.def\asdfgh\abcde\filename.csv'.

w_len1 = STRLEN( w_fiel1 ).

REPLACE ALL OCCURRENCES OF '\' IN w_fiel1 WITH '\' IN CHARACTER MODE
REPLACEMENT OFFSET w_off. "Just to get the position of  last \

w_len2 = w_len1 - w_off - 1. " Claculate the length after \
ADD 1 TO w_off.
w_fiel2 = w_fiel1+w_off(w_len2). "Copy it to another variable
WRITE: w_fiel2. "Now w_file2 is exactly the file name

faisal_altaf2
Active Contributor
0 Kudos

Hi,

Test the following Sample Code hope will solve out your problem,

DATA string TYPE string VALUE 'C:\Documents and Settings\abc.def\asdfgh\abcde\filename.csv'.
DATA string2 TYPE string.
DATA string3 TYPE string.

CALL FUNCTION 'SO_SPLIT_FILE_AND_PATH'
  EXPORTING
    full_name           = 'C:\Documents and Settings\abc.def\asdfgh\abcde\filename.csv'
 IMPORTING
   STRIPPED_NAME       = string2
   FILE_PATH           = string3
* EXCEPTIONS
*   X_ERROR             = 1
*   OTHERS              = 2
          .
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

WRITE: string3,/ string2.

Please Reply if any Issue,

Kind Regards,

Faisal

Former Member
0 Kudos

You can use the FM for your requirement,

PC_SPLIT_COMPLETE_FILENAME

Regards,

Joan

Former Member
0 Kudos

Hi,

You can use the following logic also,

DATA: it_file TYPE TABLE OF string,
      wa_file LIKE LINE OF it_file,
      w_lines TYPE i,
      w_file1 TYPE string,
      w_file2 TYPE string.
      
w_file1 = 'C:\Documents and Settings\abc.def\asdfgh\abcde\filename.csv'.

SPLIT w_file1 AT '\' INTO TABLE it_file.
DESCRIBE TABLE it_file LINES w_lines.
READ TABLE it_file INTO wa_file INDEX w_lines.

WRITE: wa_file.

Former Member
0 Kudos

Thank you all for the help!!!