Skip to Content
avatar image
Former Member

Extract filename from a path

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.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

8 Answers

  • Best Answer
    avatar image
    Former Member
    Jul 11, 2006 at 02:34 PM

    use function module: CH_SPLIT_FILENAME OR SPLIT_FILENAME.

    Add comment
    10|10000 characters needed characters exceeded

    • 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!

  • avatar image
    Former Member
    Jul 11, 2006 at 02:30 PM

    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

    split-at-into

    Regards,

    Santosh

    Message was edited by: Santosh Kumar P

    Add comment
    10|10000 characters needed characters exceeded

    • 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

  • Jul 11, 2006 at 02:36 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 11, 2006 at 02:36 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 11, 2006 at 02:56 PM

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 11, 2006 at 03:05 PM

    Hi Xavier,

    Can use the function module

    <b> (SO_SPLIT_FILE_AND_PATH)</b>.

    Hope this will help you.

    Cheers,

    Anirban.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 12, 2006 at 07:17 AM

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 12, 2006 at 08:01 AM

    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.

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

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