Skip to Content

Get filename from a fullpath

Hi,

I would like to know if there is a function module that can extract a filename from a fullpath with extension.

It's not really difficult to do such a form by myself but I would like to know if it already exist in SAP standard functions.

Regards,

Morgan

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

7 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Sep 04, 2006 at 02:52 PM

    Hi morgan,

    1. PC_SPLIT_COMPLETE_FILENAME

    2. By giving the full path to the FM,

    we get

    a) file name (only file name)

    b) also we can get the extension separately in other parameter

    regards,

    amit m.

    Add a comment
    10|10000 characters needed characters exceeded

    • Sreekanth Gollamudi Morgan Raymond Paul BOURGEOIS

      Hi,

      Use the below method.

      CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG.

      DATA: V_FILENAME   TYPE STRING,
            IT_FILETABLE TYPE FILETABLE,
            V_RC         TYPE I.
      
          CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG
            EXPORTING
              WINDOW_TITLE            = 'File Open'
              INITIAL_DIRECTORY       = 'C:'
            CHANGING
              FILE_TABLE              = IT_FILETABLE
              RC                      = V_RC
            EXCEPTIONS
              FILE_OPEN_DIALOG_FAILED = 1
              CNTL_ERROR              = 2
              ERROR_NO_GUI            = 3
              OTHERS                  = 4.
      
          IF SY-SUBRC = 0.
            READ TABLE IT_FILETABLE INDEX 1 INTO V_FILENAME.
          ELSE.
            MESSAGE E100 WITH TEXT-011. " Error while opening the file
          ENDIF.
      

      Hope this code will help you. Don't forget to reward if it is helpful for you 😉

      Thanks,

      GSK

  • author's profile photo Former Member
    Former Member
    Posted on Sep 04, 2006 at 05:41 PM

    Hi Morgan,

    Use this Function Module SO_SPLIT_FILE_AND_PATH.

    REPORT ZTEST_SHAIL4 .

    data: file_name type RLGRAP-FILENAME.

    CALL FUNCTION 'SO_SPLIT_FILE_AND_PATH'

    EXPORTING

    full_name = 'D:\SP\kun.txt'

    IMPORTING

    STRIPPED_NAME = file_name

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

    message i001(zmess) with file_name.

    Regards,

    SP.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Sep 04, 2006 at 04:25 PM

    Hi Morgan,

    Plese check this method CL_GUI_FRONTEND_SERVICES->FILE_OPEN_DIALOG.

    CALL METHOD cl_gui_frontend_services=>file_open_dialog
      EXPORTING
        window_title = 'Import from a local file'
        default_extension = 'DAT'
    * DEFAULT_FILENAME =
        initial_directory = 'C:'
    * MULTISELECTION =
    * WITH_ENCODING =
      CHANGING
        file_table = l_table
        rc = l_rc
    * USER_ACTION =
    * FILE_ENCODING =
      EXCEPTIONS
        file_open_dialog_failed = 1
        cntl_error = 2
        error_no_gui = 3
        not_supported_by_gui = 4
        others = 5.

    Hope this will help.

    Regards,

    Ferry Lianto

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Sep 04, 2006 at 05:00 PM

    Hi

    Try this code..

    Regards,

    Raj

    • Find the local directory

    l_loc = 1.

    WHILE sy-subrc EQ 0.

    SEARCH p_ofile FOR '/' STARTING AT l_loc.

    IF sy-fdpos GT 0.

    ADD sy-fdpos TO l_loc.

    l_cmd = p_ofile(l_loc).

    l_lng = 60 - l_loc.

    l_fil = p_ofile+l_loc(l_lng).

    ELSE.

    ADD 1 TO l_loc.

    ENDIF.

    ENDWHILE.

    You will get the local directory in l_cmd. (So the remaining part of p_ofile is the file)

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Sep 05, 2006 at 02:43 AM

    hi,

    We can get the file name from full path by writing a few lines of code. In case of function module, there can be a limitation to the number of characters of file name.

    try this code:

    data: v_str(50) value 'c:/first/second/myfile.doc'.
    
    types : begin of t_itab,
              record like v_str,
            end of t_itab.
    
    data: itab type table of t_itab with header line,
          v_lines type i.
    
    start-of-selection.
    
      split v_str at '/' into table itab.
      describe table itab lines v_lines.
      read table itab index v_lines.
      if sy-subrc = 0.
        write :/ 'File name :', itab-record.
      endif.

    Regards,

    Sailaja.

    Add a comment
    10|10000 characters needed characters exceeded

    • HI all,

      thanks for you're help 😊

      I can't use the open file dialog cause it will display a popup and I don't want to prompt user for filename, just to extract filename without extension from a fullpathy I already have.

      I will try other FM modules you gave me. Conerning the algoritm , I don't want to use it cause I alredy have one, just wanted to know if there is standard FM that do it but I thank you very much 😊

      regards,

      Morgan

  • Posted on Oct 30, 2015 at 01:06 PM

    Hi,

    the following function modules are useful:

    CV120_SPLIT_PATH

    CV120_SPLIT_FILE

    Good luck

    Reinhard

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Sep 16, 2016 at 02:10 PM

    This could be done using regexp (source full path in lv_fullpath)

    data: lv_root type string, lv_path type string, lv_filename type string, lv_ext type string.
             " Should match both windows frontend and application server paths
             find REGEX '^(.:\\|\/\/)(.+\\|.+\/)*(.+)\.(.+)' in lv_fullpath SUBMATCHES lv_root lv_path lv_filename lv_ext.
    

    Result would be stored in corresponding variables.

    For example:

    lv_fullpath = 'c:\temp\test\filename.txt''

    result:

    variable data lv_root c:\ lv_path temp\test lv_filename filename lv_ext txt
    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.