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

Extract filename from a path

Former Member
0 Likes
16,731

Hi folks!

Anybody knows if there is a MF to extract the filename from a path...

for example, if i have a path like:


server\filename.ext

c:filename.ext

c:\>filename.txt

i want to extract the filename: 'filename.ext' (or just, 'filename')

Thank's in advance!

Xavi.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
9,358

use function module: CH_SPLIT_FILENAME OR SPLIT_FILENAME.

Hi folks!

Anybody knows if there is a MF to extract the filename from a path...

for example, if i have a path like:


server\filename.ext

c:filename.ext

c:\>filename.txt

i want to extract the filename: 'filename.ext' (or just, 'filename')

Thank's in advance!

Xavi.

11 REPLIES 11
Read only

Former Member
0 Likes
9,358

hi,

Use <b>Split</b> statement else Use FM <b>SPLIT_FILENAME</b>

data : v_filename(100) type c value '//sap/filename.txt',
       field1(100) type c.
 
   split v_filename at '/' into field1 .

Check this out

Regards,

Santosh

Message was edited by: Santosh Kumar P

Read only

0 Likes
9,358

Please try the following code.



REPORT ZRICH_0005 .

PARAMETERS: FULLNAME TYPE RLGRAP-FILENAME .

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
* 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:/ file_name

Regards

Rich Heilman

Read only

Former Member
0 Likes
9,359

use function module: CH_SPLIT_FILENAME OR SPLIT_FILENAME.

Read only

9,358

Function CH_SPLIT_FILENAME is not good!

It is not released and obsolete (it's saved in package DIMP_OBSOLETE), but more important, it can't handle base file names which contain the '.' (dot) character in them. The logic splits the filename to basename and extension at the first occurrence of a dot, which is a mistake! It should split them at the last occurrence of a dot.

For example, if I wanted to split the file 'Budget 2016.07.01.xlsx'. the result would be like this:

Base name: 'Budget 2016'

Extension: '07.01.xlsx'

Function SPLIT_FILENAME is also useless, because it does the same mistake, and even worse, it allows only up to 3 characters in the extension - which obviously can't handle the new Office files, and other files with longer extensions. It seems the only good option is to copy CH_SPLIT_FILENAME to a custom function module and fix the mistake by myself.

Edit right after posting as I checked this FM:



Rajesh Dash wrote:

You can also try the function module 'CRM_EMAIL_SPLIT_FILENAME'. This handles long UNIX filename.

This FM works perfectly as needed, and even returns the MIME-TYPE of the file. Helpful!

Read only

Former Member
0 Likes
9,358

Hi Xavi,

1) Use this Function Module <b>SO_SPLIT_FILE_AND_PATH</b>

CALL FUNCTION 'SO_SPLIT_FILE_AND_PATH'

EXPORTING

FULL_NAME = PATH

IMPORTING

STRIPPED_NAME = FILENAME

FILE_PATH = FILEPATH

2) if you want Attributes for a file <b>EPS_GET_FILE_ATTRIBUTES</b> --> Pass in a filename and a path, and will return attributes for the file

Thanks

Sudheer

Read only

Former Member
0 Likes
9,358

Hi,

You can use the FM <b>CACS_SPLIT_PATH</b>


  Import parameters               Value          
                                                 
  I_PATH                          C:TEST.TXT                                                                                
Export parameters               Value          
                                                 
  E_PATH                          C:            
  E_FILENAME                      TEST.TXT       
  E_PSERVER                       X   

Regards

vijay

Read only

Former Member
0 Likes
9,358

Hi Xavier,

Use this Function Module.

<b>SO_SPLIT_FILE_AND_PATH</b>

Consider this code.


REPORT  ztest_2      .

PARAMETERS : p_file TYPE rlgrap-filename DEFAULT '\serverfilename.ext'.

DATA : w_fname TYPE rlgrap-filename,
       w_fpath TYPE  rlgrap-filename.


CALL FUNCTION 'SO_SPLIT_FILE_AND_PATH'
  EXPORTING
    full_name     = p_file
  IMPORTING
    stripped_name = w_fname
    file_path     = w_fpath
  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 : / p_file,
          w_fname,
          w_fpath.

Regards,

Arun Samabrgi.

Read only

Former Member
0 Likes
9,358

Hi Xavier,

Can use the function module

<b> (SO_SPLIT_FILE_AND_PATH)</b>.

Hope this will help you.

Cheers,

Anirban.

Read only

Former Member
0 Likes
9,358

Thank's to all!

Finally I used the FM SPLIT_FILENAME. It's perfect, because you can also have the filename and the extension in two different variables... just perfect!

Thank's!

Xavi.

Read only

Former Member
0 Likes
9,358

Hi Xavier,

You can also use the FM 'SO_SPLIT_FILE_AND_PATH' as Rich has mentioned. It also stores the path and the filename in separate variables... right?

CALL FUNCTION 'SO_SPLIT_FILE_AND_PATH' EXPORTING FULL_NAME = FULLNAME

IMPORTING

STRIPPED_NAME = file_name

FILE_PATH = 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.

ENDIF.

ya.. For file name and extension, you need to do the mentioned FM...

Regards,

SP.

Read only

0 Likes
9,358

You can also try the function module 'CRM_EMAIL_SPLIT_FILENAME'. This handles long UNIX filename.